12.
import java.util.Scanner;
public class Test07 {
public static void main(String[] args) {
System.out.println("请输入要使用的计算方法:1.加法 \t 2.减法 \t 3.乘法 \t 4.除法 \t 5.退出");
Scanner input = new Scanner(System.in);
int i = input.nextInt();
if (i<0 || i>4) {
System.out.print("输入有误,请重新输入:");
i = input.nextInt();
}
System.out.println("请输入数字");
int num1 = input.nextInt();
int num2 = input.nextInt();
switch (i) {
case 1:
add (num1,num2);
break;
case 2:
sub (num1,num2);
break;
case 3:
mul (num1,num2);
break;
case 4:
div(num1,num2);
break;
case 5:
System.out.println("欢迎下次使用!");
break;
}
}
public static void add (int num1,int num2) {
System.out.println(num1+"+"+num2+"="+(num1+num2));
}
public static void sub (int num1,int num2) {
System.out.println(num1+"-"+num2+"="+(num1-num2));
}
public static void mul(int num1,int num2) {
System.out.println(num1+"*"+num2+"="+(num1*num2));
}
public static void div (int num1,int num2) {
System.out.println(num1+"/"+num2+"="+(num1/num2));
}
}
2.
import java.util.Scanner;
public class Test07_02 {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("请输入第一个数:");
double num1 = input.nextDouble();
System.out.println("请输入要使用的运算符 + - * / ");
String sign = input.next();
System.out.println((sign.equals("+")));
System.out.println((sign.equals("+")) || (sign.equals("*")) || (sign.equals("-")) || (sign.equals("/")));
//判断输入的运算符是否符合规范;
// while ture 执行 当 为flase 不执行 ,当不符合规范是 为全为flase取反,全为true执行while
//当符合规范时 有一个为 true 取反flase 在&&下 退出while ;
while (!(sign.equals("+") )&&!(sign.equals("*")) && !(sign.equals("-")) && !(sign.equals("/")) )
{
System.out.println("输入错误!请输入要使用的运算符 + - * / ");
sign = input.next();
}
System.out.println("请输入第二个数:");
double num2 = input.nextDouble();
System.out.println(num1+sign+num2+"="+count(num1,sign,num2));
}
public static double count (double num1, String sign, double num2) {
double sum=0;
switch (sign) {
case "+":
sum= num1+num2;
break;
case "-":
sum= num1-num2;
break;
case "*":
sum= num1*num2;
break;
case "/":
sum= num1/num2;
break;
}
return sum;
}
}