我可以使用NumberNumberFormateXeption。即使用户输入字符串或非数字,该程序也应继续

发布于 2025-01-18 09:27:10 字数 1869 浏览 5 评论 0原文

猜数字

import java.util.*;

public class LabExer5A
{
    public static void main(String args[])
    {       
        Scanner Input = new Scanner (System.in); 
        System.out.println("Guess a number between 1 and 50");
        int JPS_Guess = Input.nextInt();
        int JPS_CorrectNum = 25;
        int JPS_NextGuess;  
    do
    {
    try
    {
            if (JPS_Guess == JPS_CorrectNum)
            {
                System.out.println("You got it in ");
            }
            else if (JPS_Guess < JPS_CorrectNum && JPS_Guess >= 1)
            {
                System.out.println("Too low. Try again.");
                JPS_NextGuess = Input.nextInt();
                JPS_Guess = JPS_NextGuess;
            }
            else if (JPS_Guess > JPS_CorrectNum && JPS_Guess <= 50)
            {
                System.out.println("Too High. Try again.");
                JPS_NextGuess = Input.nextInt();
                JPS_Guess = JPS_NextGuess;
            }
            else if(JPS_Guess < 1 || JPS_Guess > 50)
            {
                throw new InputMismatchException();
            }
    }
         catch(InputMismatchException e) 
         {
            System.out.println("Invalid value. Please enter a number between 1 and 50: ");
            JPS_NextGuess = Input.nextInt();
            JPS_Guess = JPS_NextGuess;
         }   
          catch (NumberFormatException ex) 
         {
            System.out.println("Error - Enter Numerical Values Only");
            JPS_NextGuess = Input.nextInt();
            JPS_Guess = JPS_NextGuess;
         }  
    }
    while (JPS_Guess != JPS_CorrectNum);//will rerun the program until the user guess the correct number   
}
}


}

}

我不知道我在哪里犯了错误。要么是使用了 throw catch,要么是因为我重新运行程序的方式。我对使用异常和 try-catch 还很陌生,请帮助并友善。谢谢。

Guessing the number

import java.util.*;

public class LabExer5A
{
    public static void main(String args[])
    {       
        Scanner Input = new Scanner (System.in); 
        System.out.println("Guess a number between 1 and 50");
        int JPS_Guess = Input.nextInt();
        int JPS_CorrectNum = 25;
        int JPS_NextGuess;  
    do
    {
    try
    {
            if (JPS_Guess == JPS_CorrectNum)
            {
                System.out.println("You got it in ");
            }
            else if (JPS_Guess < JPS_CorrectNum && JPS_Guess >= 1)
            {
                System.out.println("Too low. Try again.");
                JPS_NextGuess = Input.nextInt();
                JPS_Guess = JPS_NextGuess;
            }
            else if (JPS_Guess > JPS_CorrectNum && JPS_Guess <= 50)
            {
                System.out.println("Too High. Try again.");
                JPS_NextGuess = Input.nextInt();
                JPS_Guess = JPS_NextGuess;
            }
            else if(JPS_Guess < 1 || JPS_Guess > 50)
            {
                throw new InputMismatchException();
            }
    }
         catch(InputMismatchException e) 
         {
            System.out.println("Invalid value. Please enter a number between 1 and 50: ");
            JPS_NextGuess = Input.nextInt();
            JPS_Guess = JPS_NextGuess;
         }   
          catch (NumberFormatException ex) 
         {
            System.out.println("Error - Enter Numerical Values Only");
            JPS_NextGuess = Input.nextInt();
            JPS_Guess = JPS_NextGuess;
         }  
    }
    while (JPS_Guess != JPS_CorrectNum);//will rerun the program until the user guess the correct number   
}
}


}

}

I'm not sure where I made the mistake. Either the use of the throw catch or maybe because of the way I rerun the program. I'm pretty new to using exception and try-catch Please help and be kind. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧故 2025-01-25 09:27:10

nextInt()实际上输入其他数据类型时抛出InputMismatchException在非整数输入中,

另一件事是input.nextint()应始终在try/catch中。在您的代码中,第一个输入和输入任何错误的值之后,将不会捕获例外。

 catch(InputMismatchException e) 
         {
            System.out.println("Invalid value. Please enter a number between 1 and 50: ");
            JPS_NextGuess = Input.nextInt();
            JPS_Guess = JPS_NextGuess;
         }   

我修改了您的代码更简单。我建议您使用布尔值来跟踪是否猜到了这个数字,因此您不必互换猜测。此外,良好的凹痕格式有助于阅读。

import java.util.*;

public class LabExer5A
{
    public static void main(String args[])
    { 
        Scanner Input = new Scanner (System.in); 
        System.out.println("Guess a number between 1 and 50");
        int JPS_CorrectNum=25;
        boolean isGuessed=false;  
        do
        {
            try
            {
                int JPS_Guess = Input.nextInt();
                if (JPS_Guess == JPS_CorrectNum)
                {
                    System.out.println("You got it in ");
                    isGuessed=true;
                }
                else if (JPS_Guess < JPS_CorrectNum && JPS_Guess >= 1)
                {
                    System.out.println("Too low. Try again.");
                }
                else if (JPS_Guess > JPS_CorrectNum && JPS_Guess <= 50)
                {
                    System.out.println("Too High. Try again.");
                }
                else if(JPS_Guess < 1 || JPS_Guess > 50)
                {
                    throw new InputMismatchException();
                }
            }
            catch(InputMismatchException e) 
            {
                System.out.println("Invalid value. Please enter a number between 1 and 50: ");
                Input.next();
            }   
        }
        while (!isGuessed);//will rerun the program until the user guess the correct number   
    }
}

nextInt() actually throws InputMismatchException when you input other data type so you can directly use the catch for InputMismatchException and you should add Input.next() so that the scanner will take in the non integer input

Another thing is that Input.nextInt() should always be inside the try/catch. In your code, the exception will not be catched for the first input and after you input any wrong value.

 catch(InputMismatchException e) 
         {
            System.out.println("Invalid value. Please enter a number between 1 and 50: ");
            JPS_NextGuess = Input.nextInt();
            JPS_Guess = JPS_NextGuess;
         }   

I have modified your code to be simpler. I suggest you use boolean to keep track of whether the number is guessed so you don't have to interchange the guess. Also, good indentation formatting helps reading.

import java.util.*;

public class LabExer5A
{
    public static void main(String args[])
    { 
        Scanner Input = new Scanner (System.in); 
        System.out.println("Guess a number between 1 and 50");
        int JPS_CorrectNum=25;
        boolean isGuessed=false;  
        do
        {
            try
            {
                int JPS_Guess = Input.nextInt();
                if (JPS_Guess == JPS_CorrectNum)
                {
                    System.out.println("You got it in ");
                    isGuessed=true;
                }
                else if (JPS_Guess < JPS_CorrectNum && JPS_Guess >= 1)
                {
                    System.out.println("Too low. Try again.");
                }
                else if (JPS_Guess > JPS_CorrectNum && JPS_Guess <= 50)
                {
                    System.out.println("Too High. Try again.");
                }
                else if(JPS_Guess < 1 || JPS_Guess > 50)
                {
                    throw new InputMismatchException();
                }
            }
            catch(InputMismatchException e) 
            {
                System.out.println("Invalid value. Please enter a number between 1 and 50: ");
                Input.next();
            }   
        }
        while (!isGuessed);//will rerun the program until the user guess the correct number   
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文