运算

问题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);
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。