-1

我对 Java 很陌生,有一个问题。我的程序是一个简单的电脑猜谜游戏。如果你猜对了,那么你的分数会增加 1。如果你猜错了,那么你的分数会减少 1。如果你猜到 7 或 0,那么你赢/输。你能帮我理解为什么它不循环回到while语句开始的地方吗?谢谢!

import java.util.Random;
import java.util.Scanner;
import java.lang.Math;
import java.io.*;

class RandomGame {
public static void main(String str[]) throws IOException {
    Scanner scan = new Scanner(System.in);
    String userAns = new String();
    Random generator = new Random();

    int guess, num, points = 0;

    System.out.println("Hello...");
    try {
        Thread.sleep(1500);
    } catch (InterruptedException ex) {
    }
    System.out.println("Would you consider playing a fun game?");
    try {
        Thread.sleep(1000);
    } catch (InterruptedException ex) {
    }
    userAns = scan.next();
    // if/else about whether they want to play the game**********
    if (userAns.equals("yes")) {
        System.out.println("Awww yeah lets get going!");
        System.out
                .println("Objective of the game:\n1.Guess numbers out of 5\n2."
                        + "If you guess incorrect you get points reduced.\n3.Have fun!");

        try {
            Thread.sleep(5500);
        } catch (InterruptedException ex) {
        }
        System.out.println("\nReady?");
        userAns = scan.next();
        if (userAns.equals("yes")) {
            System.out.println("Okay! Here we go!");
            // COUNTDOWN************************************
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
            }
            System.out.println("3");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
            }
            System.out.println("2");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
            }
            System.out.println("1");
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
        }

        // *****************************************************
        System.out.println("Please enter a integer 1-5 to start the game.");

        guess = scan.nextInt();
        num = generator.nextInt(5) + 1;
        while (points <= 7 && points >= 0)

        {
            if (guess == num) {
                System.out.println("Correct! You gained 1 point!");
                points++;
                System.out.println(points);
            } else if (guess != num) {
                System.out.println("Incorrect. You lose 1 point.");
                points--;

            }
        }

    }

    else if (userAns.equals("no")) {
        System.out.println("GAMEOVER.\n\n\nHint: say \"yes\"");
    }

    else {
        System.out.print("yes or no answers please.");
    }
}
}
4

4 回答 4

1

您的代码确实会循环回到 while 循环开始的位置,它只是不会在下一个猜测中读取,因为您从用户输入中读取的内容位于循环之外。这两行需要在循环内

guess = scan.nextInt();
num = generator.nextInt(5) + 1;

目前,您的代码读取一个 int 并生成一个。如果猜对了你会得到7分然后赢,如果猜错了你会立即输

于 2013-10-17T03:19:56.167 回答
1

如果您第一次回答错误,您的分数变为-1。

while (points <= 7 && points >= 0)

仅检查 0 到 7 之间的范围,因此它退出循环

于 2013-10-17T03:22:08.183 回答
1

您的while循环不会等待nextInt(),因此循环只会根据第一个猜测值递增或递减。要解决此问题,请在进行计算之前等待下一个输入,并在循环中进行计算:

while ((points <= 7 && points >= 0) && scan.hasNext()) {
  // we have an input value
  guess = scan.nextInt();
  num = generator.nextInt(5) + 1;
}
于 2013-10-17T03:23:33.403 回答
0

在 while 循环中获取用户输入

  while (points <= 7 && points >= 0)
  {
      guess = scan.nextInt();
      num = generator.nextInt(5) + 1;
      // continue 

  }
于 2013-10-17T03:24:11.727 回答