当我运行这个时,输出是:
“请输入您的姓名” 我输入它
“请输入学号” 我输入它
“请输入学生姓名”
“请输入学号”
String nameChoice = "";
do{
System.out.println("Please input the students name");
String name = s.nextLine();
nameChoice = name;
System.out.println("Please enter that students number");
int studentNo = s.nextInt();
}while(!nameChoice.equalsIgnoreCase("done"));
最佳答案
这是因为 s.nextInt()
仅读取输入的整数,而不读取输入的整数后面的换行符。因此,您可以添加 s.nextLine() 来读取换行符。
所以这样做:
int studentNo = s.nextInt();
s.nextLine();
如果您不这样做,下一个 nextLine()
方法将用于读取您输入的换行符。
避免此问题的另一种方法是使用 nextLine()
读取并将其解析为整数
。
int studentNo = Integer.parseInt(s.nextLine());
关于java - do-while 循环接受一个 int 或一个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27274396/