問(wèn)題1:在Java中,除法分為取整和取余,不是直接得到計(jì)算結(jié)果的。
解決的辦法:
計(jì)算值的變量和結(jié)果值的變量類型應(yīng)該是浮點(diǎn)型,即double或者是float
// TestCompute.java 主函數(shù)
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 構(gòu)造函數(shù)
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("計(jì)算結(jié)果:" + this.result);
}
public void div(int valueOne, int valueTow) {
this.valueOne = valueOne;
this.valueTow = valueTow;
this.result = this.valueOne / this.valueTow;
System.out.print("計(jì)算結(jié)果:" + this.result);
}
}