第四章选择结构练习题2

时间:2023-02-22 21:35:48

//模拟计算器练习题

package learn;

import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
System.out.println("请输入第一个操作数:");
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
//int first=0;
//int second=0;
int first=input.nextInt();
System.out.println("请输入第二个操作数:");
int second=input.nextInt();
System.out.println("请选择运算方法:1.加法2.减法3.乘法4.除法");
int operation=input.nextInt();
switch(operation) {
case 1:
System.out.println("计算结果为:"+(first+second));
break;
case 2:
System.out.println("计算结果为:"+(first-second));
break;
case 3:
System.out.println("计算结果为:"+(first*second));
break;
case 4:
System.out.println("计算结果为:"+(first/second));
break;
}
}else {
System.out.println("请输入正确的字符");
}

   }
}