java基础–用方法来写简单计算器
简单计算器
package T9;
import java.util.Scanner;
public class Test {
public int jia(int num1,int num2) {
int sum;
sum=num1+num2;
return sum;
}
public void jian(int num1,int num2) {
System.out.println(num1-num2);
}
public void chen(int num1,int num2) {
System.out.println(num1*num2);
}
public void chu(int num1,int num2) {
System.out.println(num1/num2);
}
public static void main(String[] args) {
Test t =new Test();
Scanner wc=new Scanner(System.in);
System.out.println("输入第一个数:");
int num1=wc.nextInt();
System.out.println("输入第2个数:");
int num2=wc.nextInt();
System.out.println("+ - * /");
char op=wc.next().charAt(0);
switch (op) {
case '+':
t.jia(num1, num2);
break;
case '-':
t.jian(num1, num2);
break;
case '*':
t.chen(num1, num2);
break;
case '/':
if(num2==0)
System.out.println("除数不能为0");
else
t.chu(num1, num2);
break;
default:
System.out.println("输错了");
break;
}
}
}
|