package com.tarena.lx02;
import java.util.InputMismatchException;
import java.util.Scanner;
import org.junit.Test;
public class computer {
@Test
public void tst_comp() {
Scanner sc = new Scanner(System.in);
// 打印系统信息
printInfo();
String str = null;
int num = 0;
while (true) {
try {
str = sc.next();
num = Integer.parseInt(str);
} catch (Exception E) {
System.out.println("对不起,输入有误");
printInfo();
continue;
}
switch (num) {
case 0:
System.out.print("程序已经退出");
System.exit(-1);
case 1:
System.out.println(" 系统提示:");
try {
System.out.println(" 请输入被加数");
String str1 = sc.next();
System.out.println(" 请输入加数:");
String str2 = sc.next();
int x = Integer.parseInt(str1);
int y = Integer.parseInt(str2);
int sum = x + y;
System.out.println("你输入的" + x + "和" + y + "=" + sum);
} catch (Exception e) {
System.out.println(e.toString());
System.out.println("对不起,输入有误!");
}
printInfo();
break;
case 2:
System.out.println(" 系统提示:");
System.out.println(" 请输入被减数");
try {
String str1 = sc.next();
System.out.println(" 请输入减数:");
String str2=sc.next();
int x = Integer.parseInt(str1);
int y = Integer.parseInt(str2);
int cha = x - y;
System.out.println("你输入的" + x + "-" + y + "=" + cha);
} catch (Exception E) {
System.out.println("对不起,输入有误!");
}
printInfo();
break;
case 3:
System.out.println(" 系统提示:");
try {
System.out.println(" 请输入被乘数");
String str1 = sc.next();
System.out.println(" 请输入乘数:");
String str2 = sc.next();
int x = Integer.parseInt(str1);
int y = Integer.parseInt(str2);
int ji = x * y;
System.out.println("你输入的" + x + "*" + y + "=" + ji);
} catch (Exception E) {
System.out.println("对不起,输入有误!");
}
printInfo();
break;
case 4:
System.out.println(" 系统提示:");
System.out.println(" 请输入被除数");
try {
String str1 = sc.next();
System.out.println(" 请输入除数:");
String str2 = sc.next();
double x = Double.parseDouble(str1);
double y = Double.parseDouble(str2);
double shang = x / y;
System.out.println("你输入的" + x + "/" + y + "=" + shang);
} catch (Exception e) {
System.out.println("对不起,输入有误");
}
printInfo();
break;
case 5:
System.out.println(" 系统提示:");
System.out.println(" 请输入被除数");
try {
String str1 = sc.next();
System.out.println(" 请输入除数:");
String str2 = sc.next();
int x = Integer.parseInt(str1);
int y = Integer.parseInt(str2);
int yuShu = x % y;
System.out.println("你输入的" + x + "%" + y + "=" + yuShu);
} catch (InputMismatchException e) {
System.out.println("对不起,输入有误");
}
printInfo();
break;
default:
printInfo();
} System.out.println(" 请输入被乘数");
}
}
public static void printInfo() {
System.out
.println(" 菜单\n 1代表加法运算\n 2代表减法运算\n 3代表乘法运算\n 4代表除法运算\n 5代表取余运算\n 0代表推出程序\n");
System.out.println("请选择:");
}
}