Java 如何在猜谜游戏中继续尝试捕捉内循环
所以我的问题是,当我尝试捕捉输入错误时,我不知道如何继续我的程序。我尝试在catch语句之后使用“continue;”代码,但这只是无法控制地循环我的程序。我需要程序在用户输入错误后从停止的地方启动。任何帮助都将不胜感激。请注意,这是一项任务,但我正在通过处理代码中的垃圾来超越它Java 如何在猜谜游戏中继续尝试捕捉内循环,java,while-loop,try-catch,Java,While Loop,Try Catch,所以我的问题是,当我尝试捕捉输入错误时,我不知道如何继续我的程序。我尝试在catch语句之后使用“continue;”代码,但这只是无法控制地循环我的程序。我需要程序在用户输入错误后从停止的地方启动。任何帮助都将不胜感激。请注意,这是一项任务,但我正在通过处理代码中的垃圾来超越它 //Import library import java.io.*; import java.util.*; //File name public class G
//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;
}
}
}
//导入库
导入java.io.*;
导入java.util.*;
//文件名
公开课猜谜游戏
{
//主输入和输出错误
公共静态void main(字符串[]args)引发IOException
{
//为用户提供的投入
扫描仪输入=新扫描仪(系统输入);
扫描器i=新扫描器(System.in);
//循环变量、随机数、字符和计数器
int guess=0;
int rnd;
字符决策;
布尔循环=假;
//随机数发生器
随机=新随机();
rnd=random.nextInt(100)+1;
//循环猜测和输入
while(!loop){
试一试{
系统输出打印项次(rnd);
//提示用户
System.out.println(“请猜一个介于1-100之间的数字,按0退出”);
int num=in.nextInt();
//如果语句
如果(num==0)
{
//当用户键入“0”时,它将结束程序
系统出口(0);
System.out.println(“你放弃了!…重置程序…”);
}
否则如果(num>rnd)
{
//打印太大,增加了计数器“猜测”
System.out.println(“数字太大了!”);
猜测++;
}
else if(numScanner默认情况下按空格分割标准输入,并保留已解析的子字符串数量的索引。您调用的特定方法(.nextWhatever)将尝试将行中的下一个字符串解析为其预期类型,并且仅在成功时增加索引;如果没有要解析的流,则将等待新的输入
循环无限的原因是它无法将令牌解析为整数,并且没有增加索引。有两种方法可以跳过无效的输入。nextLine()
将跳过等待的流的其余部分。例如,如果输入为“1 abc 2”
但是,如果您想继续尝试后续令牌(在本例中跳过“abc”,但尝试“2”,这是有效的),next()
更合适,因为它只跳过一个令牌
try(){
// validate input
int num = in.nextInt();
}
catch(Exception e){
System.out.println("Ignoring your faulty input");
in.next();
}
尝试此移动您的追赶,因为您只测试输入。另外,在追赶中添加.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;
}
在.next()中放入
在您的捕获中。nextInt
在您没有输入有效的int时失败,但您从未告诉scanner移过它。此外,在循环结束时不需要继续;不过这不是一个坏猜测。顺便说一句,一旦您完成此工作,我建议发布代码以获得一些良好的建设性反馈。我从阅读他们的建议,应该会有所帮助。
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;
}