[英]How to continue loop inside of Try Catch for Guessing game
所以我的问题是,当我尝试捕获输入错误时,我不知道如何继续执行我的程序。 我尝试使用“继续”; 代码在我的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;
}
}
}
扫描程序默认情况下按空格分隔标准输入,并保留已解析多少个子字符串的索引。 您调用的特定方法(.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()
更合适,因为它只会跳过一个令牌。
try(){
// validate input
int num = in.nextInt();
}
catch(Exception e){
System.out.println("Ignoring your faulty input");
in.next();
}
尝试此举以赶上进度,因为您仅测试输入。 还可以在捕获中添加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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.