package bdqn;
import java.util.Scanner;
public class Calculator {
/*
* 定义一个计算器类,包括加、减、乘、除运算。
*/
int number1;
int number2;
public void add() {
System.out.println("" + this.number1 + "+" + this.number2 + "=" + (this.number1 + this.number2));
}
public void sub() {
System.out.println("" + this.number1 + "-" + this.number2 + "=" + (this.number1 - this.number2));
}
public void mul() {
if (this.number2 != 0) {
System.out.println("" + this.number1 + "/" + this.number2 + "=" + (this.number1 / this.number2));
} else {
System.out.println("除数不能为0!!");
}
}
public void div() {
System.out.println("" + this.number1 + "%" + this.number2 + "=" + (this.number1 % this.number2));
}
public static void main(String[] args) {
Calculator num = new Calculator();
Scanner input = new Scanner(System.in);
System.out.print("请输入一个数:");
num.number1 = input.nextInt();
System.out.print("请输入另一个数:");
num.number2 = input.nextInt();
num.add();
num.sub();
num.mul();
num.div();
}
}
一键复制
编辑
Web IDE
原始数据
按行查看
历史