while循环在每次输入后都不会停止

时间:2015-08-27 02:48:45

标签: java while-loop

好的伙计们,我有一个问题,这个循环停止。只要我输入一些东西,它就会再次循环。该程序的要点是猜测一个随机数,如果猜测较低则显示"太低"如果高于随机数显示"太高"并且如果make 7尝试或者正确的话,给他们7次尝试并杀死游戏。请帮忙

我的输出

I'm thinking of a number between 1-100.  You have 7 guesses.
First guess: 
20
Sorry too low
Guess 1:
20
Guess 2:
20
Sorry, you didn't guess it in 7 tries.  You lose.
Sorry too low
Guess 3:
22
Guess 4:
2
Sorry, you didn't guess it in 7 tries.  You lose.
Sorry too low
Guess 5:
22
Guess 6:
2
Sorry too low
Guess 7:
2
Guess 8:

输出应为:

I'm thinking of a number between 1-100.  You have 7 guesses.
First guess: 1
Sorry, you are too low.
Guess # 2: 2
Sorry, you are too low.
Guess # 3: -8
Sorry, you are too low.
Guess # 4: 0
Sorry, you are too low.
Guess # 5: 7
Sorry, you are too low.
Guess # 6: 612
Sorry, that guess is too high.
Guess # 7: -523
Sorry, you didn't guess it in 7 tries.  You lose.

代码:

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

public class App1
{
public static void main( String[] args )
{
    Scanner keyboard = new Scanner(System.in);
    Random r = new Random();

    int secretNumber = 1 + r.nextInt(100);
    int guesses;
    int tries = 0;

    System.out.println("I'm thinking of a number between 1-100.  You have 7        guesses.");
    System.out.println("First guess: ");
    guesses = keyboard.nextInt();
    tries++;


    while(guesses != secretNumber && guesses < secretNumber ){
        System.out.println("Sorry too low");
        System.out.println("Guess " + tries + ":");
        guesses = keyboard.nextInt();
        tries++;

    while(guesses != secretNumber && guesses > secretNumber )
        System.out.println("Sorry too high");
        System.out.println("Guess " + tries + ":");
        guesses = keyboard.nextInt();

        tries++;

    if (tries <7){
        System.out.println("Sorry, you didn't guess it in 7 tries.  You     lose.");
    }

    }
}
}

3 个答案:

答案 0 :(得分:1)

使用单个while循环,并使用if语句检查输入,如下所示:

while(guesses != secretNumber){   
  if(guesses != secretNumber && guesses < secretNumber ){
    System.out.println("Sorry too low");
    System.out.println("Guess " + tries + ":");
    guesses = keyboard.nextInt();
    tries++;}

  else if(guesses != secretNumber && guesses > secretNumber ){
    System.out.println("Sorry too high");
    System.out.println("Guess " + tries + ":");
    guesses = keyboard.nextInt();
    tries++;}

  if (tries <7){
    System.out.println("Sorry, you didn't guess it in 7 tries.  You     lose.");
    break;
  }
}

答案 1 :(得分:1)

您必须了解范围和程序流程的更多信息。但是,好的尝试。以下代码将修复您的代码。

public static void main( String[] args )
{
    Scanner keyboard = new Scanner(System.in);
    Random r = new Random();

    int secretNumber = 1 + r.nextInt(100);
    int guesses = 0;
    int tries = 0;

    System.out.println("I'm thinking of a number between 1-100.  You have 7        guesses.");
    while(guesses != secretNumber && tries < 7) {
        System.out.println("Guess #"+tries+": ");
        guesses = keyboard.nextInt();
        if(guesses == secretNumber) {
            System.out.println("You win");
            break;
        } else if(guesses < secretNumber) {
            System.out.println("Sorry too low");
        } else {
            System.out.println("Sorry too high");
        }
        tries++;
        if(tries >= 7) {
            System.out.println("Sorry, you didn't guess it in 7 tries.  You lose.");
        }
    }
}

答案 2 :(得分:0)

您可以使用for循环仅执行一定次数的代码。

for (int guessNum = 1; guessNum < 8; guessNum++)
    // do stuff

以上代码将运行7次,变量guessNum将保留您的“猜测编号”。如果您想在7次猜测结束前结束循环,可以在break循环体中使用for语句。因此,如果用户没有正确猜测,则循环继续。如果他们这样做,你就会“脱离”循环并且猜测结束。

这是我的演绎,只是为了完整性:

Scanner cin = new Scanner(System.in);
Random rng = new Random();

int secretNumber = 1 + rng.nextInt(100);
System.out.println("I'm thinking of a number between 1-100.");

boolean success = false;
for (int guessNum = 1; guessNum < 8; guessNum++) {
    System.out.println("Guess #" + guessNum + ". You have " + (7 - guessNum + 1) + " attempts remaining.");
    int guess = cin.nextInt();
    if (guess < secretNumber) {
        System.out.println("Too low!");
    } else if (guess > secretNumber) {
        System.out.println("Too high!");
    } else {
        System.out.println("You guessed right! Have a cookie!");
        success = true;
        break;
    }
}

if (!success)
    System.out.println("Better luck next time.");

cin.close();