java calculator类_Calculator.java
package bdqn;
import java.util.Scanner;
public class Calculator {
/*
* 定義一個(gè)計(jì)算器類,包括加、減、乘、除運(yùn)算。
*/
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("除數(shù)不能為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("請(qǐng)輸入一個(gè)數(shù):");
num.number1 = input.nextInt();
System.out.print("請(qǐng)輸入另一個(gè)數(shù):");
num.number2 = input.nextInt();
num.add();
num.sub();
num.mul();
num.div();
}
}
一鍵復(fù)制
編輯
Web IDE
原始數(shù)據(jù)
按行查看
歷史
總結(jié)
以上是生活随笔為你收集整理的java calculator类_Calculator.java的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 在线教育:直播授课有哪些注意事项呢?
- 下一篇: 解决固网 HU-1608n 更换主板后不