为什么我的while循环冻结我的程序以及如何修复它?

时间:2022-04-23 08:44:59

I am very new to Java and have a question. My program is a simple guessing game with the computer. If you guess it correctly, then your points move up by 1. If you guess incorrectly, then your points decrease by 1. If you get to 7 or 0 then you win/lose. Could you help me understand why it doesnt loop back to where the while statement starts? Thanks!

我是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 个解决方案

#1


1  

Your code does loop back to where the while loop starts, It just doesn't read in the next guess as your read from the users input is outside the loop. These two lines need to be inside the loop

你的代码会循环回到while循环开始的地方,它只是在下一个猜测中没有读取,因为你从用户输入的读取是在循环之外。这两行必须在循环内

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

Currently your code reads in one int and generates one. If the guess is correct you'll get 7 points and then win, if its wrong you'll lose immediatly

目前,您的代码读入一个int并生成一个。如果猜测是正确的,你将得到7分然后获胜,如果错误你将立即失去

#2


1  

If you get the answer wrong the first time, your points become -1.

如果你第一次得到答案错误,你的积分将变为-1。

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

Checks for ranges only between 0 and 7 and hence it quits the loop

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

#3


1  

Your while loop isn't going to wait for the nextInt(), so the loop just increments or deincrements based on what the first guess was. To fix this, wait for the next input before doing a calculation, and do the calculations within the loop:

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

while ((points <= 7 && points >= 0) && scan.hasNext()) {
  // we have an input value
  guess = scan.nextInt();
  num = generator.nextInt(5) + 1;
}

#4


0  

take user input inside the while loop

在while循环中输入用户输入

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

  }

#1


1  

Your code does loop back to where the while loop starts, It just doesn't read in the next guess as your read from the users input is outside the loop. These two lines need to be inside the loop

你的代码会循环回到while循环开始的地方,它只是在下一个猜测中没有读取,因为你从用户输入的读取是在循环之外。这两行必须在循环内

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

Currently your code reads in one int and generates one. If the guess is correct you'll get 7 points and then win, if its wrong you'll lose immediatly

目前,您的代码读入一个int并生成一个。如果猜测是正确的,你将得到7分然后获胜,如果错误你将立即失去

#2


1  

If you get the answer wrong the first time, your points become -1.

如果你第一次得到答案错误,你的积分将变为-1。

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

Checks for ranges only between 0 and 7 and hence it quits the loop

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

#3


1  

Your while loop isn't going to wait for the nextInt(), so the loop just increments or deincrements based on what the first guess was. To fix this, wait for the next input before doing a calculation, and do the calculations within the loop:

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

while ((points <= 7 && points >= 0) && scan.hasNext()) {
  // we have an input value
  guess = scan.nextInt();
  num = generator.nextInt(5) + 1;
}

#4


0  

take user input inside the while loop

在while循环中输入用户输入

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

  }