- import java.util.Scanner;
- import java.lang.Exception;
- class Program
- {
- public static void main(String[] args)
- { Scanner sc = new Scanner(System.in);
- try
- {
- System.out.println("请输入一个数A:");
- int a = sc.nextInt();
- System.out.println("请选择符号:+,-,*,/");
- String strOperate = sc.nextLine();
- System.out.println("您输入的是:"+a + strOperate);
- System.out.println("请输入一个数B:");
- int b = sc.nextInt();
- int strResult = 0;
- switch (strOperate)
- {
- case "+" :
- strResult = a + b;
- break;
- case "-":
- strResult = a - b;
- case "*":
- strResult = a * b;
- break;
- case "/":
- if ( b == 0)
- System.out.println("除数不能为0");
- else
- strResult = a / b;
- break;
- }
- System.out.println("结果是:"+strResult);
- }
- catch (Exception ex)
- {
- System.out.println("您的输入有错:"+ex.getMessage());
- }
- }
- }
复制代码 |
|