Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中输入错误时如何使程序重新启动_Java - Fatal编程技术网

在java中输入错误时如何使程序重新启动

在java中输入错误时如何使程序重新启动,java,Java,在java中,如果用户输入错误,如何使程序在你问他们的问题时重新启动?例如,在我下面发布的代码中,我得到了一个问题:选择一个介于0-9之间的数字。如果用户输入错误的输入,如10或更高或字符串,如何使程序在该问题时重新启动。我将非常感谢任何提示或建议 import java.util.Scanner; public class Project2 { public static void main(String[] args) { String PlayAgain = "

在java中,如果用户输入错误,如何使程序在你问他们的问题时重新启动?例如,在我下面发布的代码中,我得到了一个问题:选择一个介于0-9之间的数字。如果用户输入错误的输入,如10或更高或字符串,如何使程序在该问题时重新启动。我将非常感谢任何提示或建议

import java.util.Scanner;

public class Project2 {

    public static void main(String[] args) {
        String PlayAgain = "Y";

        while (PlayAgain.equals("Y")) {
            // Generate a secret Numbers
            int number1 = (int) (Math.random() * 10);
            int number2 = (int) (Math.random() * 10);
            int number3 = (int) (Math.random() * 10);
            int arr[] = new int[3];
            arr[0] = number1;
            arr[1] = number2;
            arr[2] = number3;

            boolean g1;
            boolean g2;
            boolean g3;

            g1 = false;
            g2 = false;
            g3 = false;

            // Show secret numbers
            System.out.println(arr[0] + ", " + arr[1] + ", " + arr[2]);

            Scanner input = new Scanner(System.in);

            // Prompt the user to enter a guess

            System.out.print("Enter your lottery pick (0-9): ");
            int guess1 = input.nextInt();

            if (guess1 > 9) {
                System.out
                        .println("Wrong input enter only 1 number between 0-9: ");
            } else {

            }

            System.out.print("Enter your lottery pick (0-9): ");
            int guess2 = input.nextInt();

            if (guess2 > 9) {
                System.out
                        .println("Wrong input enter only 1 number between 0-9: ");
            } else {

            }

            System.out.print("Enter your lottery pick (0-9): ");
            int guess3 = input.nextInt();

            if (guess3 > 9) {
                System.out
                        .println("Wrong input enter only 1 number between 0-9: ");
            } else {
                continue;

            }

            // Show secret numbers and your numbers
            System.out.println("The Secret Numbers are " + arr[0] + (", ")
                    + arr[1] + (", ") + arr[2]);
            System.out.println("Your Numbers are " + guess1 + (", ") + guess2
                    + (", ") + guess3);

            if (guess1 == arr[0] && guess2 == arr[1] && guess3 == arr[2]) {
                System.out
                        .println("Three Matching in Exact order, You Won $100,000 ");
            } else if ((

            (guess1 == arr[1] || guess1 == arr[2])
                    && (guess2 == arr[0] || guess2 == arr[2]) && (guess3 == arr[0] || guess3 == arr[1]))

                    ||

                    ((guess1 == arr[0])
                            && (guess2 == arr[1] || guess2 == arr[2]) && (guess3 == arr[1] || guess3 == arr[2]))

                    ||

                    ((guess2 == arr[1])
                            && (guess3 == arr[0] || guess3 == arr[1]) && (guess1 == arr[0] || guess1 == arr[2]))

                    ||

                    ((guess3 == arr[2])
                            && (guess2 == arr[0] || guess2 == arr[2]) && (guess1 == arr[1] || guess1 == arr[0])))

            {
                System.out
                        .println("Three Matching in Different order, You Won $10,000 ");
            } else if (

            ((guess1 == arr[0] || guess1 == arr[1] || guess1 == arr[2]) && (guess2 == arr[0]
                    || guess2 == arr[1] || guess2 == arr[2]))
                    && (guess3 != arr[0] || guess3 != arr[1] || guess3 != arr[2])

                    ||

                    ((guess1 == arr[0] || guess1 == arr[1] || guess1 == arr[2]) && (guess3 == arr[0]
                            || guess3 == arr[1] || guess3 == arr[2]))
                    && (guess2 != arr[0] || guess2 != arr[1] || guess2 != arr[2])

                    ||

                    ((guess2 == arr[0] || guess2 == arr[1] || guess2 == arr[2]) && (guess3 == arr[0]
                            || guess3 == arr[1] || guess3 == arr[2]))
                    && (guess1 != arr[0] || guess1 != arr[1] || guess1 != arr[2])

            ) {
                System.out.println("Any Two Numbers Matching, You Win $1,000 ");
            } else if ((guess1 == arr[0] || guess1 == arr[1] || guess1 == arr[2])
                    && (guess2 != arr[0] || guess2 != arr[1] || guess2 != arr[2])
                    && (guess3 != arr[0] || guess3 != arr[1] || guess3 != arr[2])

                    ||

                    (guess2 == arr[0] || guess2 == arr[1] || guess2 == arr[2])
                    && (guess1 != arr[0] || guess1 != arr[1] || guess1 != arr[2])
                    && (guess3 != arr[0] || guess3 != arr[1] || guess3 != arr[2])

                    ||

                    (guess3 == arr[0] || guess3 == arr[1] || guess3 == arr[2])
                    && (guess1 != arr[0] || guess1 != arr[1] || guess1 != arr[2])
                    && (guess2 != arr[0] || guess2 != arr[1] || guess2 != arr[2])

            )// close if
            {
                System.out.println("Any One Number Matching, You Win $10 ");
            } else {
                System.out.println("You Lose Sorry ");
            }

            System.out.println("Would you like to play again? Y/N ");
            PlayAgain = input.next();
            PlayAgain = PlayAgain.toUpperCase();

        }// end of while loop
    } // end of main

} // end of class

抱歉,如果代码很难阅读,我是这方面的初学者,在发布之前我确实使用了ctrl+shift+f来格式化代码。

最好将一些逻辑分解为单独的方法来处理代码重用。但是,为了使其与您显示的内容保持一致,并使示例易于阅读,以下是您可以在输入数字时执行的操作:

        boolean isValid = false;
        while (!isValid) {
            System.out.print("Enter your lottery pick (0-9): ");

            int guess1 = -1;
            try {
                guess1 = input.nextInt();
            } catch (Exception e) {
                // Read any pending input which would be invalid characters at this point
                input.next();
            }

            if (guess1 > 9 || guess1 < 0) {
                System.out.println("Wrong input enter only 1 number between 0-9: ");
            } else {
                isValid = true;
            }
        }
boolean isValid=false;
而(!isValid){
系统输出打印(“输入您的彩票选择(0-9):”;
int-1=-1;
试一试{
guess1=input.nextInt();
}捕获(例外e){
//读取此时将是无效字符的任何挂起输入
input.next();
}
如果(猜测1>9 | |猜测1<0){
System.out.println(“输入错误,仅输入0-9之间的1个数字:”);
}否则{
isValid=true;
}
}
您的代码“不雅”。尝试使用如下模式:

伪java代码:

while (true) {
    // get input
    if (input is "exit")
        break;
    if (input is bad)
        continue;
    // process input
}

我知道你的程序是一个巨大的主要方法。一个需要考虑的问题是重构代码并创建一个接受输入的方法,该方法能够检测输入是否有效,如果无效,保持提示并在输入有效之前不从方法返回。您的答案在大多数情况下有效,但每次都会重新启动并给出新的数字,不会继续下两个问题。答案部分您可能会给出提示,将catch()逻辑更改为读取input.next()。我以前没有让它在输入字母字符时刷新数据。希望有帮助。我用这段代码来代替第一个问题fine(还必须将“int guess1=-1”移到while循环之外,以便用于下面的代码)。