简体   繁体   中英

Why won't my Driver execute the while loop?

I am working on a project for class which asks me to produce two versions of a guessing game using while and for loops. The last part of the assignment is to create a Driver class to call on the two versions of the game made in a GuessGame class. For some reason my Driver when run will not execute the while loop to allow the user to input their choice to choose what version of the game to use. Any help?

GuessGame code:

import java.util.Scanner;

public class GuessGame
{
    public static void example()
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Give me a number:");
        int num = in.nextInt();
        System.out.println("You gave me " + num);
    }

    public static void roll1()
    {
        int guess = 1;
        int randomNumber = (int) (Math.random() * 10) + 1;
        Scanner scan = new Scanner(System.in);
        boolean win = true;

        System.out.println("I'm thinking of a number between 1 and 10.");

        while (win)
        {
            System.out.println("What is your guess?");
            guess = scan.nextInt();
            if (guess == randomNumber)
            {
                System.out.println("You guessed it!");
                break;
            }
            else if (guess > randomNumber)
            {
                System.out.println("Smaller!");
            }
            else if (guess < randomNumber)
            {
                System.out.println("Bigger!");
            }
        }
    }

    public static void roll2(int num)
    {
        int guess = 0;
        int randomNum = (int) (Math.random() * 20) + 5;
        Scanner scan = new Scanner(System.in);
        boolean win = true;

        System.out.println("I'm thinking of a number between 5 and 25");

        for (int guessNum = 0; guessNum < num; guessNum++)
        {
            System.out.println("What is your guess?");
            guess = scan.nextInt();
            if (guess == randomNum)
            {
                System.out.println("You guessed it!");
                break;
            }
            else if (guess > randomNum)
            {
                System.out.println("Smaller!");
            }
            else if (guess < randomNum)
            {
                System.out.println("Bigger!");
            }
        }
    }
}

Driver code:

import java.util.Scanner;

public class Driver
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);

        GuessGame game = new GuessGame();

        int choice = 1;

        System.out.println("Guessing Game");
        System.out.println("-------------");

        while (choice != 1)
        {
            System.out.println("1. Version 1");
            System.out.println("2. Version 2");
            System.out.println("3. Quit");

            System.out.println("\nPlease enter your choice > ");
            choice = in.nextInt();
            System.out.println();

            switch (choice)
            {
                case 1:
                    System.out.println("Version 1:");
                    game.roll1();
                    break;
                case 2:
                    System.out.println("Version 2:");
                    game.roll2(5);
                    break;
                case 3:
                    System.out.println("Thanks for playing.");
                    break;
                default:
                    System.out.println("Invalid choice.");
            }
        }
    }
}

Sorry for formatting issues, first time using this site.

Your while condition is never true;

you declare choice as

int choice = 1;

and then you say:

while (choice != 1)
{
   //stuff
}

since choice is declared as = 1, the loop never completes itself. An alternative would be to declare choice as follows

int choice = -1;

another would be to use a do while construct.

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