java—当输出为false时如何重复“if”语句

qnzebej0  于 2021-07-14  发布在  Java
关注(0)|答案(7)|浏览(333)

我正在做一个简单的游戏,用户必须猜测一个随机数。我已经设置了所有的代码,除了一个事实,即如果猜测太高或太低,我不知道如何让他们重新输入一个数字,并继续玩,直到他们得到它。它只是停止;代码如下:

import java.util.Scanner;
import java.util.Random;

public class Test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Random rand = new Random();

        int random = rand.nextInt(10) + 1;

        System.out.print("Pick a number 1-10: ");
        int number = input.nextInt();

        if (number == random) {
            System.out.println("Good!");
        } else if (number > random) {
            System.out.println("Too Big");
        } else if (number < random) {
            System.out.println("Too Small");
        }
    }
}
fafcakar

fafcakar1#

将if语句括在do while循环中,该循环将在用户没有猜到数字时循环:

int number;
 do {
    System.out.print("Pick a number 1-10: ");
    number = input.nextInt();

    if (number == random) {
        System.out.println("Good!");
    } else if (number > random) {
        System.out.println("Too Big");
    } else if (number < random) {
        System.out.println("Too Small");
    }
 } while (number != random);
dxxyhpgq

dxxyhpgq2#

为了有条件地重复代码,请使用循环。

// this code will execute only once
System.out.print("Pick a number 1-10: ");
// initialize number to a value that would not be used and not equal random
int number = -1;

// the code inside the curly braces will repeat until number == random
while (number != random) {
    // get next number
    number = input.nextInt();
    // handle case one
    if(number > random) System.out.println("Too Big");
    // handle case two
    if(number < random) System.out.println("Too Small");
}
// once number == random, the condition is false so we break out of the loop
System.out.println("Good!");
vs91vp4v

vs91vp4v3#

您要寻找的是编程语言中的结构,它允许您一次又一次地做特定的事情。
这是使用循环完成的。例如,检查while循环的文档。这就是你需要的。

qcuzuvrc

qcuzuvrc4#

为了重复任何事情,你需要一个循环。
在循环体中间的条件得到满足之前,一种常见的重复方法是构建一个无限循环,并添加一种方法来打破它。
在java中生成无限循环的惯用方法是 while(true) :

while (true) {
    System.out.print("Pick a number 1-10: ");
    int number = input.nextInt();
    if (number == random) {
        System.out.println("Good!");
        break; // This ends the loop
    } else if (number > random) {
        System.out.println("Too Big");
    } else if (number < random) {
        System.out.println("Too Small");
    }
}

此循环将继续其迭代,直到代码路径到达 break 声明。

gev0vcfq

gev0vcfq5#

你需要使用一个循环。代码应该是这样工作的:

import java.util.Scanner;
import java.util.Random;

public class Test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Random rand = new Random();

        int random = rand.nextInt(10) + 1;

        System.out.print("Pick a number 1-10: ");
        int number = input.nextInt();

        boolean found = false;
        while (!found) {
           if (number == random) {
                System.out.println("Good!");
               found = true;
            } else if (number > random) {
                System.out.println("Too Big, try again:");
                number = input.nextInt();
            } else if (number < random) {
                System.out.println("Too Small, try again:");
                number = input.nextInt();
            }
        }
    }
}
cpjpxq1n

cpjpxq1n6#

你需要一个 do...while .

Random rand = new Random();
int random = rand.nextInt(10) + 1;
do {
  Scanner input = new Scanner(System.in);

  System.out.print("Pick a number 1-10: ");
  int number = input.nextInt();

  if (number == random) {
    System.out.println("Good!");
  } else if (number > random) {
    System.out.println("Too Big");
  } else if (number < random) {
    System.out.println("Too Small");
  }

} while ( number != random );
zf2sa74q

zf2sa74q7#

有几种技术可以循环您的请求,其中包括: while (<condition>) { <do something> } do { } while (); for (<init statement>, <condition>, <update statement>) { <do something> } 为了展示这一点,您可以通过使用递归避免使用上述显式循环构造之一:

mport java.util.Scanner;
import java.util.Random;

public class Test {

    public static void ask(int random) {
        Scanner input = new Scanner(System.in);
        System.out.print("Pick a number 1-10: ");
        int number = input.nextInt();

        if (number == random) {
            System.out.println("Good!");
        } else if (number > random) {
            System.out.println("Too Big");
            ask(random);
        } else if (number < random) {
            System.out.println("Too Small");
            ask(random);
        }
    }

    public static void main(String[] args) {
        Random rand = new Random();

        int random = rand.nextInt(10) + 1;
        ask(random);
    }
}

在这里 ask() 方法不断调用自身,直到达到最终条件(用户猜对了)。
根据java虚拟机的聪明程度,这可能会给调用堆栈带来压力,也可能不会。

相关问题