所以我的问题是,当我尝试并捕获输入错误时,我不知道如何继续我的程序。我尝试过使用“继续”;我的catch语句之后的代码,但这只是无法控制地循环我的程序。我需要程序在用户输入错误后从中断处开始。任何帮助,将不胜感激。请注意,这是一项任务,但我已经超越了处理代码中的垃圾。
//Import library
import java.io.*;
import java.util.*;
//File name
public class GuessingGame
{
//Main throws Input and output error
public static void main (String [] args) throws IOException
{
//inputs for users
Scanner in = new Scanner (System.in);
Scanner i = new Scanner (System.in);
//variables for the loop, random number, character and counter
int guess = 0;
int rnd;
char decision;
boolean loop = false;
//random number generator
Random random = new Random();
rnd = random.nextInt(100) + 1;
//loops the guess and input
while (!loop){
try{
System.out.println(rnd);
//prompt the user
System.out.println(" Please guess a number between 1-100. Press 0 to exit.");
int num = in.nextInt();
//if statements
if (num==0)
{
//when user types '0' it ends the program
System.exit(0);
System.out.println("You gave up!.... Reseting program...");
}
else if (num>rnd)
{
//prints too big, adds to counter 'guess'
System.out.println("The number is too big!");
guess++;
}
else if (num<rnd)
{
//prints too small, adds to counter 'guess'
System.out.println("The number is too small!");
guess++;
}
else
{
//prints correct, adds to counter, dsiplays # of guesses and ends loop
System.out.println("You guessed the number right!!");
guess++;
System.out.print(" # of guesses: " + guess);
//Note**: i could've just put 'break;' but the compiler would'nt read the rest of the code below
loop = true;
//loops the case untill correct input is chosen either 'Y' or 'N'
while(true){
//prompt the user if they want to play again
System.out.println(" Would you like to play again? Y/N?");
decision = i.nextLine().charAt(0);
switch (decision) {
case 'Y':
case 'y':
//calls main, basically restarts the game
GuessingGame.main(args);
break;
case 'N':
case 'n':
System.out.println("Bye!");
//exits the program completely
System.exit(0);
break;
default:
//if incorrect input, this prints
System.out.println("Please enter a Yes or No <Y/N>");
}
}
}
}
//catches input errors
catch (Exception e){
System.out.println("Only numbers!!!");
//GuessingGame.main(args);
continue;
}
}
}
答案 0 :(得分:1)
默认情况下,扫描程序将空格分割为标准输入,并保留已解析的子字符串数的索引。您调用的特定方法(.nextWhatever)将尝试将下一个字符串解析为其预期类型,并且只有在成功时才会增加索引;如果没有要解析的流,它将等待新的输入。
循环无限的原因是因为它无法将标记解析为整数并且没有增加索引。有两种方法可以跳过无效输入。 nextLine()
将跳过剩余的流等待。例如,如果输入为“1 abc 2”
in.nextInt(); // equals 1
// in.nextInt() would fail
in.nextLine(); // equals "abc 2" and if put in your catch would clear the stream
但是,如果你想继续尝试后续令牌(在这种情况下跳过“abc”但尝试“2”,这是有效的),next()
更合适,因为它只会跳过一个令牌。 / p>
try(){
// validate input
int num = in.nextInt();
}
catch(Exception e){
System.out.println("Ignoring your faulty input");
in.next();
}
答案 1 :(得分:1)
尝试此举动,因为您只是测试输入。还要在你的catch中添加in.nextLine()以消耗掉留下的角色。
while (!loop){
int num;
try{
System.out.println(rnd);
//prompt the user
System.out.println(" Please guess a number between 1-100. Press 0 to exit.");
num = in.nextInt();
}
catch (Exception e){
System.out.println("Only numbers!!!");
//GuessingGame.main(args);
in.nextLine();
continue;
}