Java 如何制作只循环 3 次的循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32773581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:03:44  来源:igfitidea点击:

How to make a loop that only loops 3 times

javaloops

提问by Trevn Jones

I'm a very new Java programmer and I was messing around with the stuff that I knew. I created this Guessing Game program but I only want to allow the user to be able to guess 3 times before displaying that the user has failed and what the number was. Here is my code:

我是一个非常新的 Java 程序员,我正在处理我所知道的东西。我创建了这个 Guessing Game 程序,但我只想让用户能够猜 3 次,然后才能显示用户失败以及数字是多少。这是我的代码:

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

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

        int random = rand.nextInt(20);

        while (true) {
            System.out.print("Enter a number between 1-20: ");
            int number = input.nextInt();

            if (number > random) {
                System.out.println("Too Big");
                System.out.println("");
            } else if (number < random) {
                System.out.println("Too Small");
                System.out.println("");
            } else if (number == random) {
                System.out.print('\f');
                System.out.println("Correct!");
                break;
            }
        }
    }
}

回答by Morteza Azizi

You can use for loop instead or declare counter as integer and check in while loop if it is bigger than 3 just exit from while statement.

您可以改用 for 循环或将 counter 声明为整数,如果它大于 3 则检查 while 循环,只需退出 while 语句。

回答by M3579

You need a forloop.

你需要一个for循环。

for (int i = 0; i < 3; i++) {
    // your code goes here
}

What this does is it initializes a variable called ito 0, then loops while iis less than 3, and adds 1 to iafter each loop. This loop should loop 3 times (i= 0: loop, i= 1: loop, i= 2: loop, i= 3: stop loop, since iis no longer less than 3).

它的作用是初始化一个名为i0的变量,然后循环 whilei小于 3,并i在每个循环后加1 。这个循环应该循环3次(i= 0:循环,i= 1:循环,i= 2:循环,i= 3:停止循环,因为i不再小于3)。

回答by Elliott Frisch

If I understand your question, you would like your game to play 3 times. Create a nested loop. Something like,

如果我理解你的问题,你希望你的游戏玩 3 次。创建嵌套循环。就像是,

for (int i = 0; i < 3; i++) {
    int random = rand.nextInt(20);
    while (true) {
        // ...
    }
}

To only allow three guesses, count the number of guesses. Something like,

要只允许三个猜测,请计算guesses的数量。就像是,

int guess = 0;
int random = rand.nextInt(20);
while (guess < 3) {
    System.out.print("Enter a number between 1-20: ");
    int number = input.nextInt();
    if (number > random) {
        System.out.println("Too Big");
        System.out.println("");
    } else if (number < random) {
        System.out.println("Too Small");
        System.out.println("");
    } else if (number == random) {
        System.out.print('\f');
        System.out.println("Correct!");
        break;
    }
    guess++;
}

回答by jason12459

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

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

        int random = rand.nextInt(20);

        for(int i=0; i<3;i++) {
            System.out.print("Enter a number between 1-20: ");
            int number = input.nextInt();

            if (number > random) {
                System.out.println("Too Big");
                System.out.println("");
            } else if (number < random) {
                System.out.println("Too Small");
                System.out.println("");
            } else if (number == random) {
                System.out.print('\f');
                System.out.println("Correct!");
                break;
            }
        }
    }
}