【发布时间】:2012-01-01 22:46:13
【问题描述】:
我正在尝试了解如何只接受来自用户的数字,并且我尝试使用 try catch 块来这样做,但我仍然会遇到错误。
Scanner scan = new Scanner(System.in);
boolean bidding;
int startbid;
int bid;
bidding = true;
System.out.println("Alright folks, who wants this unit?" +
"\nHow much. How much. How much money where?" );
startbid = scan.nextInt();
try{
while(bidding){
System.out.println("$" + startbid + "! Whose going to bid higher?");
startbid =+ scan.nextInt();
}
}catch(NumberFormatException nfe){
System.out.println("Please enter a bid");
}
我试图了解它为什么不起作用。
我通过在控制台中输入 a 对其进行了测试,我会收到一个错误,而不是充满希望的“请输入出价”解决方案。
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Auction.test.main(test.java:25)
【问题讨论】:
标签: java input try-catch java.util.scanner integer