简体   繁体   中英

Guessing Game, with a While loop

i'm currently trying to create a while loop for my program, a Guessing game. 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. When the number has been guessed, the User can press 1, to close, anything else to continue running the loop again.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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