import java.util.*;
public class TestIn {
public static void main(String[] args) {
boolean b = true;
while(b) {
Scanner s = new Scanner(System.in);
System.out.print("“1”:继续 “2”:退出 请输入: ");
if(s.nextInt() == 1) continue;
if(s.nextInt() == 2) {
b = false;
}
}
}
}
为什么每次得输入两次“2”才能退出
java
------解决方案--------------------
s.nextInt()因为这个出现了两次,一个nextInt代表一次输入
把s.nextInt()用一个变量接收,然后下面判断的时候用变量就可以了
------解决方案--------------------
应该是nextInt执行了2次
把代码改成
试试
import java.util.*;
public class TestIn {
public static void main(String[] args) {
boolean b = true;
while(b) {
Scanner s = new Scanner(System.in);
System.out.print("“1”:继续 “2”:退出 请输入: ");
int num = s.nextInt();
if(num == 1) continue;
if(num == 2) {
b = false;
}
}
}
}