简体   繁体   English

猜游戏,带有While循环

[英]Guessing Game, with a While loop

i'm currently trying to create a while loop for my program, a Guessing game. 我目前正在尝试为我的程序(猜测游戏)创建一个while循环。 I've set it up so the user can create a max value ie 1-500 and then the user can proceed to guess the number. 我进行了设置,以便用户可以创建最大值(即1-500),然后用户可以继续猜测该数字。 When the number has been guessed, the User can press 1, to close, anything else to continue running the loop again. 猜到数字后,用户可以按数字1来关闭,然后再按其他任何按钮即可继续运行循环。

My problem, is that the code gives me an error when trying to continue the loop, no compiling errros 我的问题是,代码在尝试继续循环时给了我一个错误,没有编译错误

This is my Code: 这是我的代码:

import java.util.Random;
import java.util.Scanner;
public class Gættespil2
{
    public static void main(String[] args)
    {
        Random rand = new Random();
        int TAL = rand.nextInt(20) + 1;
        int FORSØG = 0;
        Scanner input = new Scanner (System.in);
        int guess;
        int loft;
        boolean win = false;

        boolean keepPlaying = true;
        while ( keepPlaying )
        {
            Scanner tastatur = new Scanner(System.in);
            System.out.print("Indsæt loftets højeste værdi : ");
            loft = tastatur.nextInt();
            TAL = (int) (Math.random() * loft + 1);

            while (win == false) 
            {

                System.out.println(" Gæt et tal mellem 1 og  "+ loft + "):: ");
                guess = input.nextInt();
                FORSØG++;
                if (guess == TAL) 
                {

                    win = true;
                }
                else if (guess < TAL) 
                {
                    System.out.println("Koldere, gæt igen");
                }
                else if (guess > TAL) {
                    System.out.println("Varmere, Gæt igen!!");
                }
            }
            System.out.println(" Tillykke du vandt...endeligt!!! ");
            System.out.println(" tallet var" + TAL);
            System.out.println(" du brugte " + FORSØG + " forsøg");

            System.out.println("Slut spillet? tast 1.");
            System.out.println("tryk på hvadsomhelst for at spille videre");
            int userInt = input.nextInt();
            if( userInt == 1)
            {
                keepPlaying = false;
            }
        }
    }
}

Simple answer. 简单的答案。 You didn't initialize all the necessary values within your 'keepPlaying' loop before beginning a second round after the player successfully completed the first round. 在玩家成功完成第一轮比赛之后的第二轮比赛之前,您没有在'keepPlaying'循环中初始化所有必要的值。 See annotations to your code, below: 请在下面查看代码的注释:

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

public class GuessingGame
{
    public static void main(String[] args)
    {

        Random rand = new Random();
        int TAL = rand.nextInt(20) + 1;
        int FORSØG = 0;
        Scanner input = new Scanner (System.in);
        int guess;
        int loft;
        boolean win = false;

        boolean keepPlaying = true;
        while ( keepPlaying )
        {
            Scanner tastatur = new Scanner(System.in);
            System.out.print("Enter a maximum limit: ");
            loft = tastatur.nextInt();
            TAL = (int) (Math.random() * loft + 1);

            // *** LOOK HERE ***

            // Reset the 'win' flag here, otherwise the player receives an
            // automatic win on all subsequent rounds following the first

            win = false;

            while (win == false) 
            {
                System.out.println("Guess the number between one and "+ loft + "):: ");
                guess = input.nextInt();
                FORSØG++;
                if (guess == TAL) 
                {

                    win = true;
                }
                    else if (guess < TAL) 
                {
                System.out.println("Colder, guess again!");
                }
                    else if (guess > TAL) {
                    System.out.println("Warmer, guess again!");
                }
            }

            System.out.println("You've found the number!");
            System.out.println("The number was: " + TAL + ".");
            System.out.println("You guessed " + FORSØG + " times.");

            System.out.println("To quit, enter 1.");
            System.out.println("Provide any other input to play again.");

            int userInt = input.nextInt();

            if( userInt == 1)
            {
                keepPlaying = false;
            }
        }
    }
}

Sorry for the translation into English -- I had to make sure I was reading things correctly. 抱歉,无法翻译成英文-我必须确保自己阅读正确。 You might also want to substitute "higher" and "lower" for "warmer" and "colder." 您可能还想用“较高”和“较低”代替“较暖”和“冷淡”。 "Warmer" and "colder" tend to suggest a proximity to the correct answer, as opposed to the direction in which that correct answer lies. “温暖的”和“冷淡的”倾向于暗示接近正确答案,而不是正确答案所处的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM