尝试在 while 循环中使用 Try catch 直到用户正确回答而不输入无效数据(Java) [英] Trying to use Try catch in a while loop until the user answers correctly without entering invalid data(Java)

查看:24
本文介绍了尝试在 while 循环中使用 Try catch 直到用户正确回答而不输入无效数据(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个程序,该程序要求用户猜测一个随机生成的数字,并在输入正确时退出循环.我还试图阻止用户输入无效数据并希望循环重复直到用户输入有效数据.问题是当输入字母作为输入时,程序会重复.感谢您提前提供帮助.我正在使用日食开普勒

I am trying to create a program which asks the user to guess a number which is randomly generated and the loop exits when the input is correct. I am also trying to stop user from entering an invalid data and want the loop to repeat until user enters a valid data. The problem is when is type in an alphabet as an input, the program repeats. thank you for helping in advance. I am using eclipse kepler

输出:
尝试猜数字:克您输入了无效数据.请再试一次尝试猜数字:您输入了无效数据.请再试一次尝试猜数字:您输入了无效数据.请再试一次尝试猜数字:您输入了无效数据.请再试一次尝试猜数字:您输入了无效数据.请再试一次尝试猜数字:您输入了无效数据.请再试一次尝试猜数字:您输入了无效数据.请再试一次尝试猜数字:您输入了无效数据.请再试一次尝试猜数字:您输入了无效数据.请再试一次尝试猜数字:您输入了无效数据.请再试一次尝试猜数字:

Output:
Try guessing the number: k You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number:

while(true){
         try{
            System.out.println("Try guessing the number: ");
            guess=input.nextInt();
            if(guess==sum){
                System.out.println("You have guessed it correctly");
                break;
            }
        }catch(InputMismatchException e){
            System.out.println("You have entered invalid data. Please try again");
        }
       }

推荐答案

这对我有用...它修复了 while(true) 问题并使用更简单的方法来检查更容易调试的错误数据!祝您游戏愉快!

This worked for me... It fixes the while(true) issue and uses a simpler way to check for incorrect data that is easier to debug! Enjoy and good luck with your game!

        String guessString="";
        int guess = 0;
        Scanner input = new Scanner(System.in);
        int sum = 12;
        //just used 12 as a placeholder, you'll have to connect your 
        //random number generator, as well as changing the default of 
        //guess=0 if 0 is in your range for the random number

        while(sum!=guess){

            System.out.println("Try guessing the number: ");
            guessString=input.next();
            try {
                guess = Integer.parseInt(guessString);
            } catch(Exception e) {
                System.out.println("Invalid Data.");
                guess=0;
            }

        }
        System.out.println("You have guessed it correctly");

这篇关于尝试在 while 循环中使用 Try catch 直到用户正确回答而不输入无效数据(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆