*编辑:好的,在解决了try catch错误之后,在打印时catch {..中出现了问题。
*,基本上,当我说我想再次玩游戏时,它将继续游戏,但它还会打印第一个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会生成一个数字,用户必须猜测它。我大部分使用扫描器类。我遇到的麻烦是创建一个try catch来检查用户输入是否为有效的Integer。

最终发生的事情是它下面的代码块无法识别已经初始化的变量。

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();
            }
        }
    }
}

最佳答案

在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
}

09-09 22:00
查看更多