标签:alt main efault result dde lang ast scanner cas
public class MethodDemo11 {
public static void main(String[] args) {
judge();
}
//加法
public static double add(double x,double y) {
return x + y;
}
//减法
public static double sub(double x,double y) {
return x - y;
}
public static double mul(double x,double y) {
return x * y;
}
public static double div(double x,double y) {
return x / y;
}
//判断是哪个方法
public static void judge() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入两个数:");
double x = sc.nextDouble();
double y = sc.nextDouble();
System.out.println("请输入运算符(+,-,*,/):");
String operator = sc.next();
double result = 0;
switch ( operator ) {
case "+":
result = add(x,y);
System.out.println(x + "+" + y + "=" + result);
break;
case "-":
result = sub(x,y);
System.out.println(x + "-" + y + "=" + result);
break;
case "*":
result = mul(x,y);
System.out.println(x + "*" + y + "=" + result);
break;
case "/":
if ( y == 0 ) {
System.out.println("第二个数字不能为0,否则除法运算没有意义");
} else {
result = div(x,y);
System.out.println(x + "*" + y + "=" + result);
}
break;
default:
System.out.println("你输入的是非法字符");
}
}
}
输入的不是四个运算符时
输入的是 + 时:
输入的是 - 时:
输入的是 * 时:
输入的是 / 时:
y 为 0时,输入的是 / 时:
标签:alt main efault result dde lang ast scanner cas
原文地址:https://www.cnblogs.com/duoruic/p/14934841.html