提问人:jim_bug 提问时间:10/6/2023 最后编辑:DevilsHnd - 退したjim_bug 更新时间:10/6/2023 访问量:70
当我尝试获取输入数字时,Scanner 类的方法如何表现?
How do the methods of the Scanner class behave when I try to take input numbers?
问:
我正在研究 Scanner 类的方法,只是每个站点都说了一些完全不同的东西,我感到困惑。我的疑问如下:
为什么是以下代码:
System.out.println("Enter the number of wheels: ");
do {
if (input.hasNextInt()) {
numeroRuote = input.nextInt();
// verificaTipoDiDato = false;
break;
}
else {
System.out.println("Wrong data type for number of wheels!\Enter new number of wheels: ");
// verificaTipoDiDato = true;
input.next();
}
}
while (!input.hasNextInt());
System.out.println("Enter the number of headlights: ");
do {
if (input.hasNextInt()) {
numeroFari = input.nextInt();
// verificaTipoDiDato = false;
break;
}
else {
System.out.println("Incorrect data type for number of headlights!\Enter new number of headlights: ");
// verificaTipoDiDato = true;
input.next();
}
}
while (!input.hasNextInt());
它这样做:
Enter the number of wheels:
hello
Data type for the wrong number of wheels!
Enter a new number of wheels:
hello
Data type for the wrong number of wheels!
Enter a new number of wheels:
4
Enter the number of headlights:
Vehicle wheel number: 0
Number of vehicle headlights: 4
难道不应该也需要输入大灯的数量吗?
我不明白为什么。
答: 暂无答案
上一个:扫描仪未到达最后一行
评论
input.hasNextInt()
nextInt()