简体   繁体   English

为什么我的驱动程序不执行 while 循环?

[英]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.我正在做一个课堂项目,该项目要求我使用 while 和 for 循环生成两个版本的猜谜游戏。 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.作业的最后一部分是创建一个 Driver 类来调用 GuessGame 类中制作的两个版本的游戏。 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.出于某种原因,我的驱动程序在运行时不会执行 while 循环以允许用户输入他们的选择来选择要使用的游戏版本。 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;您的 while 条件永远不会为真;

you declare choice as你宣布choice

int choice = 1;

and then you say:然后你说:

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

since choice is declared as = 1, the loop never completes itself.由于选择声明为 = 1,循环永远不会自行完成。 An alternative would be to declare choice as follows另一种选择是声明如下选择

int choice = -1;

another would be to use a do while construct.另一种方法是使用do while构造。

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

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