Do While循环和嵌套if语句

时间:2017-04-04 20:17:53

标签: java

我正在尝试编写一个随机生成数字的程序,用户会尝试猜测数字。该程序会计算猜测正确数字所需的尝试次数。我遇到的问题是越过while循环。当我运行程序时,它不会超过while循环,直到我点击-1,在这种情况下它会输出"Your number is too low"。我不确定我哪里出错了。

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

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

    int number1, userInput; //assign vars
    int tries = 0;

    Random rand = new Random();     
    Scanner keyboard = new Scanner(System.in);  //define Random and scanner input 


    System.out.println("Welcome to the Guessing Game!");
    System.out.println("-----------------------------");

    number1 = rand.nextInt(20) + 1;

    do
    {
        System.out.println("\nI'm thinking of a number between 1 and 20. Enter -1 if you would like to quit");
        userInput = keyboard.nextInt();
        tries++;
    }
    while(userInput != -1);
    {
        if (userInput > 0 && userInput < 21)
        {
            if(userInput == number1)
            {
                System.out.println("That is the correct number");
                tries++;
                System.out.println("The Number of tries: " + tries);
                System.out.println("Game Over.");

            }
            else if(userInput > number1)
            {
                System.out.println("Your number is too high");
                System.out.println("Please try again");
                userInput = keyboard.nextInt();
                tries++;
            }
            else if(userInput < number1);
            {
                System.out.println("Your number is too low");
                System.out.println("Please try again");
                userInput = keyboard.nextInt();
                tries++;
            }
        }
        else
        {
            System.out.println("Please enter a number between 1 and 20");
        }

    }
    System.out.println("The number of tries: " + tries);
}

}

4 个答案:

答案 0 :(得分:0)

你的do / while循环没有做你想到的。你有:

do {
    System.out.println("\nI'm thinking of a number between 1 and 20. Enter -1 if you would like to quit");
    userInput = keyboard.nextInt();
    tries++;
} while(userInput != -1);

此点以下的代码在do while循环之后执行,因此当您到达此处时userInput将始终为-1!

答案 1 :(得分:0)

你的do-while循环之后会有一对额外的括号,以及一个if语句后面的分号,它会改变程序的流程。

else if(userInput < number1);

答案 2 :(得分:0)

你的牙套嵌套和放置令人困惑。难怪你有问题。

学会更加注重风格和可读性。

我没有编译或运行你的代码。我没有深入思考你是否正确地做到了这一点。但我可能更喜欢这样做:

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

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

    int number1, userInput; //assign vars
    int tries = 0;

    Random rand = new Random();     
    Scanner keyboard = new Scanner(System.in);  //define Random and scanner input 

    System.out.println("Welcome to the Guessing Game!");
    System.out.println("-----------------------------");

    number1 = rand.nextInt(20) + 1;

    do
    {
        System.out.println("\nI'm thinking of a number between 1 and 20. Enter -1 if you would like to quit");
        userInput = keyboard.nextInt();
        tries++;
         if (userInput > 0 && userInput < 21) {
            if(userInput == number1) {
                System.out.println("That is the correct number");
                System.out.println("The Number of tries: " + tries);
                System.out.println("Game Over.");
                userInput = -1;
            } else if(userInput > number1) {
                System.out.println("Your number is too high");
                System.out.println("Please try again");
            } else if(userInput < number1) {
                System.out.println("Your number is too low");
                System.out.println("Please try again");
            }
        } else {
            System.out.println("Please enter a number between 1 and 20");
        }
    } while(userInput != -1);
    System.out.println("The number of tries: " + tries);
}

答案 3 :(得分:-1)

我认为你的do .... while循环应该看起来像这样

do
    {
        System.out.println("\nI'm thinking of a number between 1 and 20. Enter -1 if you would like to quit");
        userInput = keyboard.nextInt();
        tries++;

if (userInput > 0 && userInput < 21)
        {
            if(userInput == number1)
            {
                System.out.println("That is the correct number");
                tries++;
                System.out.println("The Number of tries: " + tries);
                System.out.println("Game Over.");

            }
            else if(userInput > number1)
            {
                System.out.println("Your number is too high");
                System.out.println("Please try again");
                userInput = keyboard.nextInt();
                tries++;
            }
            else if(userInput < number1);
            {
                System.out.println("Your number is too low");
                System.out.println("Please try again");
                userInput = keyboard.nextInt();
                tries++;
            }
        }
        else
        {
            System.out.println("Please enter a number between 1 and 20");
        }
    }
    while(userInput != -1);