Java Try-and-Catch用户输入会导致变量未初始化
*编辑:好的,在修复try-catch错误后,Java Try-and-Catch用户输入会导致变量未初始化,java,variables,input,initialization,try-catch,Java,Variables,Input,Initialization,Try Catch,*编辑:好的,在修复try-catch错误后,catch{..打印时出现问题。 *,基本上,当我说我想再玩一次时,它会继续游戏,但它也会打印第一个捕获,然后在第23行请求输入 if (decision.equalsIgnoreCase("yes")) { ai = (int)(Math.random()*101); System.out.println("From 0 to 100, what number do you think
catch{..
打印时出现问题。
*,基本上,当我说我想再玩一次时,它会继续游戏,但它也会打印第一个捕获
,然后在第23行请求输入
if (decision.equalsIgnoreCase("yes"))
{
ai = (int)(Math.random()*101);
System.out.println("From 0 to 100, what number do you think I have generated?");
tryCatch = true;
loop = true;
rtrn = true;
while (tryCatch == true)
{
while (loop == true)
{
try
{
guess = Integer.parseInt(iConsole.nextLine());
if (guess >= 0)
{
loop = false;
}
}
catch (NumberFormatException e)
{
System.out.println("Invalid input. Please try again.");
}
catch (InputMismatchException e)
{
System.out.println("Invalid input. Please try again!");
}
}
嗨,这是我的第一篇文章,所以如果我在论坛上的代码格式错误,我会编辑它
现在我正在用java eclipse编写一个游戏,cpu生成一个数字,用户必须猜测它。我正在使用scanner类完成大部分工作。我遇到的问题是创建一个try-catch来检查用户输入是否为有效整数
最终的结果是,它下面的代码块无法识别已经初始化的变量
package ics3U;
import java.util.*;
import java.io.*;
public class highLow
{
static public void main (String args[]) throws IOException
{
String name;
String decision;
String decision2;
int ai;
int guess;
int counter = 1;
boolean fullGame = true;
boolean tryCatch = true;
boolean rtrn = true;
Scanner iConsole = new Scanner(System.in);
System.out.println("Hello! Welcome to HiLo!");
System.out.println("What is your full name?");
name = iConsole.nextLine();
System.out.println("Hello " + name + "! Would you like to play?");
decision = iConsole.nextLine();
while (fullGame == true)
{
if (decision.equalsIgnoreCase("yes"))
{
ai = (int)(Math.random()*101);
System.out.println("From 0 to 100, what number do you think I have generated?");
tryCatch = true;
rtrn = true;
while (tryCatch == true)
{
try
{
guess = Integer.parseInt(iConsole.nextLine());
}
catch (Exception e)
{
System.out.println("Invalid input. Please try again.");
}
while (guess != ai)
{
if (guess < ai)
{
System.out.println("Too low!");
guess = iConsole.nextInt();
}
else if (guess > ai)
{
System.out.println("Too high!");
guess = iConsole.nextInt();
}
counter = counter + 1;
}
System.out.println("Correct! You guessed it after " + counter + " tries!");
counter = ((counter - counter)+1);
System.out.println("Would you like to play again?");
while (rtrn == true)
{
decision2 = iConsole.next(); //finally..
if (decision2.equalsIgnoreCase("yes"))
{
fullGame = true;
tryCatch = false;
rtrn = false;
break; //do-while may be needed, have to bypass catch, 'break' works after restating value of tryCatch & rtrn
}
else if (decision2.equalsIgnoreCase("no"))
{
System.out.println("Goodbye.");
fullGame = false;
tryCatch = false;
rtrn = false;
iConsole.close();
}
else
{
System.out.println("Sorry?");
}
}
/*catch (Exception e)
{
System.out.println("Invalid input. Please try again.");
}
catch (NumberFormatException e)
{
System.out.println("Invalid input. Please try again.");
}
//More specific Exceptions, turn this on later
catch (InputMismatchException e)
{
System.out.println("Invalid input. Please try again!");
}*/
}
}
else if (decision.equalsIgnoreCase("no"))
{
System.out.println("Goodbye.");
fullGame = false;
tryCatch = false;
rtrn = false;
iConsole.close();
}
else
{
System.out.println("Sorry?");
decision = iConsole.nextLine();
}
}
}
}
包ics3U;
导入java.util.*;
导入java.io.*;
公共级高低
{
静态公共void main(字符串args[])引发IOException
{
字符串名;
字符串决策;
字符串决策2;
国际人工智能;
智力猜测;
int计数器=1;
布尔fullGame=true;
布尔tryCatch=true;
布尔rtrn=真;
扫描仪iConsole=新扫描仪(System.in);
System.out.println(“你好!欢迎来到HiLo!”);
System.out.println(“您的全名是什么?”);
name=iConsole.nextLine();
System.out.println(“你好”+name+“!你想玩吗?”);
decision=iConsole.nextLine();
while(fullGame==true)
{
如果(判决等信号情况(“是”))
{
ai=(int)(Math.random()*101);
System.out.println(“从0到100,您认为我生成了多少?”;
tryCatch=true;
rtrn=真;
while(tryCatch==true)
{
尝试
{
guess=Integer.parseInt(iConsole.nextLine());
}
捕获(例外e)
{
System.out.println(“输入无效,请重试”);
}
while(猜!=ai)
{
如果(猜测人工智能)
{
System.out.println(“太高了!”);
guess=iConsole.nextInt();
}
计数器=计数器+1;
}
System.out.println(“正确!您在“+counter+”尝试之后猜到了!”);
计数器=((计数器-计数器)+1);
System.out.println(“您想再次播放吗?”);
while(rtrn==true)
{
decision2=iConsole.next();//最终。。
如果(决定2.同等信号情况(“是”))
{
fullGame=true;
tryCatch=false;
rtrn=假;
break;//可能需要时执行,必须绕过捕获,在重新启动tryCatch&rtrn的值后,“break”起作用
}
否则,如果(第2号决定,同等信号情况(“否”))
{
System.out.println(“再见”);
fullGame=false;
tryCatch=false;
rtrn=假;
iConsole.close();
}
其他的
{
System.out.println(“对不起?”);
}
}
/*捕获(例外e)
{
System.out.println(“输入无效,请重试”);
}
捕获(数字格式)
{
System.out.println(“输入无效,请重试”);
}
//更具体的例外情况,请稍后启用
捕获(输入不匹配异常e)
{
System.out.println(“输入无效,请重试!”);
}*/
}
}
否则,如果(判决。同等信号情况(“否”))
{
System.out.println(“再见”);
fullGame=false;
tryCatch=false;
rtrn=假;
iConsole.close();
}
其他的
{
System.out.println(“对不起?”);
decision=iConsole.nextLine();
}
}
}
}
由于语句位于try块中,它们有可能失败,并且您的程序有可能尝试使用未初始化的变量。解决方案是将变量初始化为有意义的默认值,即
int guess = -1; // some default value
您还应该将while循环环绕try/catch块。在输入的数据有效之前,不要让程序进行
boolean validGuess = false;
while (!validGuess) {
// prompt user for input here
try {
guess = Integer.parseInt(iConsole.nextLine());
if (/* .... test if guess is valid int */ ) {
validGuess = true;
}
} catch (NumberFormatException e) {
// notify user of bad input, that he should try again
}
}
如果您需要在整个程序中执行类似的操作,您甚至可以将所有这些都封装到它自己的方法中。在catch块中添加一个
continue
语句。这样,如果用户输入的不是整数的内容而解析失败,它将立即重试,而不是尝试运行循环的其余部分
try
{
guess = Integer.parseInt(iConsole.nextLine());
}
catch (Exception e)
{
System.out.println("Invalid input. Please try again.");
continue; // jump to beginning of loop
}
尝试将所有代码移到catch块(在循环中)之后的Try块内,移到此行之后
guess = Integer.parseInt(iConsole.nextLine());
正如您目前所拥有的,只要parseInt中出现异常,它仍然会尝试处理t