问题1:在Java中,除法分为取整和取余,不是直接得到计算结果的。
解决的办法:
计算值的变量和结果值的变量类型应该是浮点型,即double
或者是float
// TestCompute.java 主函数
package demo1;
public class TestCompute {
public static void main(String[] args) {
// TODO Auto-generated method stub
GoCompute comput = new GoCompute();
comput.add(11,25);
comput.div(3,8);
}
}
// GoCompute.java 构造函数
package demo1;
public class GoCompute {
double valueOne;
double valueTow;
double result;
public void add(int valueOne, int valueTow) {
this.valueOne = valueOne;
this.valueTow = valueTow;
this.result = this.valueOne + this.valueTow;
System.out.println("valueOne:" + this.valueOne);
System.out.println("valueTow:" + this.valueTow);
System.out.println("计算结果:" + this.result);
}
public void div(int valueOne, int valueTow) {
this.valueOne = valueOne;
this.valueTow = valueTow;
this.result = this.valueOne / this.valueTow;
System.out.print("计算结果:" + this.result);
}
}