Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 陷入了从内部循环if语句的困境_Java_Loops_If Statement - Fatal编程技术网

Java 陷入了从内部循环if语句的困境

Java 陷入了从内部循环if语句的困境,java,loops,if-statement,Java,Loops,If Statement,我目前正在做一个类似老虎机的项目。我遇到了需要循环IF语句的问题,但只有在用户输入为YES时,才需要在该循环中循环?下面是我正在尝试做的一些示例代码 int num1 = 0; int num2 = 0; int num3 = 0; Scanner scan = new Scanner(System.in); Random numRand = new Random(); num1 = numRand.nextInt((9 - 0) + 1); num2 = numRand.nextInt

我目前正在做一个类似老虎机的项目。我遇到了需要循环IF语句的问题,但只有在用户输入为YES时,才需要在该循环中循环?下面是我正在尝试做的一些示例代码

int num1 = 0;
int num2 = 0;
int num3 = 0;

Scanner scan = new Scanner(System.in);  
Random numRand = new Random();

num1 = numRand.nextInt((9 - 0) + 1);
num2 = numRand.nextInt((9 - 0) + 1);
num3 = numRand.nextInt((9 - 0) + 1);

System.out.println(num1 + " " + num2  + " " + num3);

if(num1 == num2 && num1 == num3 && num2 == num3) {
    System.out.println("All three match - jackpot");
    System.out.printf("Would you like to play again? ");
    String Yes = scan.nextLine();
    if(Yes.equals("y")) {

    }
    String No = scan.nextLine();
    if(No.equals("n")) {

    }
}
else if(num1 == num2 || num2 == num3 || num1 == num3) {
    System.out.println("Two number match");
    System.out.printf("Would you like to play again? ");
    String Yes = scan.nextLine();
    if(Yes.equals("y")) {

    }
    String No = scan.nextLine();
    if(No.equals("n")) {

    }
}
else {
    System.out.println("No numbers match");
}
scan.close();
从我的代码中,您可以看到,在IF语句中,当用户的输入=Y时,我尝试运行另一个IF语句(对于yes),这样,如果用户在提示时输入Y,则IF语句将循环

结果表明,如果所有3个数字都匹配,则输出: 如果两个数字匹配,则输出: 如果没有匹配的数字,则输出:

我希望你能理解。递归 有两种方法可以做到这一点,一种是通过。例如,如果所有这些代码都存在于具有此签名的函数中

public static void main(String[] args)
您只需添加一个呼叫即可,例如

    if(Yes.equals("y")) {
        main(new String[0]); // String[0] is just to satisfy the arguments of the main function, if yours requires other arguments, put them there
    }
这样做的缺点是,您的代码将创建扫描仪等。在某些时候,您将看到异常,因为您将耗尽堆栈

2.While循环 另一个选项是将所有代码放入
while
构造中,并使用
continue
再次执行内部所有代码,然后使用
break
退出循环:

while (true) {
    // S: loop start
    num1 = numRand.nextInt((9 - 0) + 1);
    . . .
    if(Yes.equals("y")) {
        continue; // this will cause us to go to S 
    } else {
        break; // this will cause us to go to A
    }
}
// A: after loop
1.递归 有两种方法可以做到这一点,一种是通过。例如,如果所有这些代码都存在于具有此签名的函数中

public static void main(String[] args)
您只需添加一个呼叫即可,例如

    if(Yes.equals("y")) {
        main(new String[0]); // String[0] is just to satisfy the arguments of the main function, if yours requires other arguments, put them there
    }
这样做的缺点是,您的代码将创建扫描仪等。在某些时候,您将看到异常,因为您将耗尽堆栈

2.While循环 另一个选项是将所有代码放入
while
构造中,并使用
continue
再次执行内部所有代码,然后使用
break
退出循环:

while (true) {
    // S: loop start
    num1 = numRand.nextInt((9 - 0) + 1);
    . . .
    if(Yes.equals("y")) {
        continue; // this will cause us to go to S 
    } else {
        break; // this will cause us to go to A
    }
}
// A: after loop

我建议一个
do while
构造

Scanner scan = new Scanner(System.in);  
Random numRand = new Random();

boolean keep_playing = true;

do
{
    num1 = numRand.nextInt((9 - 0) + 1);
    num2 = numRand.nextInt((9 - 0) + 1);
    num3 = numRand.nextInt((9 - 0) + 1);

    System.out.println(num1 + " " + num2  + " " + num3);

    if(num1 == num2 && num1 == num3) { // && num2 == num3  - unnecessary
        System.out.println("All three match - jackpot");
    }
    else if(num1 == num2 || num2 == num3 || num1 == num3) {
        System.out.println("Two number match");
    }
    else {
        System.out.println("No numbers match");
    }

    System.out.printf("Would you like to play again? ");

    String input = scan.nextLine();
    keep_playing = input.equals("y");

} while ( keep_playing )

scan.close();

我建议一个
do while
构造

Scanner scan = new Scanner(System.in);  
Random numRand = new Random();

boolean keep_playing = true;

do
{
    num1 = numRand.nextInt((9 - 0) + 1);
    num2 = numRand.nextInt((9 - 0) + 1);
    num3 = numRand.nextInt((9 - 0) + 1);

    System.out.println(num1 + " " + num2  + " " + num3);

    if(num1 == num2 && num1 == num3) { // && num2 == num3  - unnecessary
        System.out.println("All three match - jackpot");
    }
    else if(num1 == num2 || num2 == num3 || num1 == num3) {
        System.out.println("Two number match");
    }
    else {
        System.out.println("No numbers match");
    }

    System.out.printf("Would you like to play again? ");

    String input = scan.nextLine();
    keep_playing = input.equals("y");

} while ( keep_playing )

scan.close();

查看
while
循环:同时,查看
break
语句。要进一步澄清@RafiduzzamanSonnet评论,请了解一些do…while循环。开始简单,例如,尝试提示用户输入一个数字,直到他们输入9。另外,在if语句之外思考一下,它是否真的是必需的,或者是否可以以某种方式将其用作循环条件?祝你好运查看
while
循环:同时,查看
break
语句。要进一步澄清@RafiduzzamanSonnet评论,请了解一些do…while循环。开始简单,例如,尝试提示用户输入一个数字,直到他们输入9。另外,在if语句之外思考一下,它是否真的是必需的,或者是否可以以某种方式将其用作循环条件?祝你好运你也可以使用布尔变量而不是无限循环,有些人觉得这更容易阅读。使用递归方法确实帮了大忙,但我后来还是遇到了一些问题,循环的工作方式与我想要的!不过非常感谢:)你也可以使用布尔变量而不是无限循环,有些人觉得这更容易阅读。使用递归方法确实帮了大忙,但我后来还是遇到了一些问题,循环的工作方式与我希望的一样!非常感谢:)非常感谢!这是正确的方法,你帮了我大忙@SeanHub没问题,很乐意帮忙。非常感谢!这是正确的方法,你帮了我大忙@SeanHub没问题,很乐意帮忙。