我试着在IDE上运行它,但它就是不运行。只有输入数字0才能运行它。它不能离开循环吗?
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
do {
switch(number) {
case 1:
System.out.println("Language selection");
break;
case 2:
System.out.println("Customer support");
break;
case 3:
System.out.println("Check account balance");
break;
case 4:
System.out.println("Check loan balance");
break;
}
}
while(number != 0);
System.out.println("Exit");
}
}
2条答案
按热度按时间0h4hbjxa1#
number
的初始化应该在前面完成。下面是代码:2wnc66cl2#
两个变化:将
scanner.nextInt()
行移动到do
内部,并将while
条件从number != 0
更改为number < 1 || number > 4
: