package com.company;
import java.util.*;
import java.lang.*;
public class Main {
public static void main (String[] args) {
System.out.println("Enter Any one number from following list to carry out Equation ");
System.out.println("1.Add ");
System.out.println("2.Subtract ");
System.out.println("3.Divide ");
System.out.println("4.Multiply ");
Scanner in=new Scanner(System.in);
int read=in.nextInt();
System.out.println("Enter 2 number for Equation ");
double a=in.nextDouble();
double b=in.nextDouble();
double r;
switch (read) {
case 1 -> r = a + b;
case 2 -> r = a - b;
case 3 -> r = a / b;
case 4 -> r = a * b;
default -> {
System.out.println("Error! Select from 1 to 4 ");
return;
}
}
System.out.println("Result of Equation is: "+r);
}
}
如果我选择大于4的任何数字。开关盒未退出,并要求用户输入2次,并在输入数字后退出开关盒。
如果我选择大于4的数字,该怎么办才能使开关盒退出?
最佳答案
如果希望跳过a
和b
的输入,则执行顺序是错误的
尝试添加条件if块
double a;
double b;
if( 0 < r && r < 5) {
a = in.nextDouble();
b = in.nextDouble();
}
关于java - Java菜单程序切换的情况下,如果从菜单中选择了数字,则避免退出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62180982/