简体   繁体   English

布尔值不会 WHILE 循环 java 编程

[英]boolean value will not WHILE loop java programming

I'm having an issue I haven't ran into yet.我遇到了一个我还没有遇到的问题。 I wrote a guessing game in java to guess a number between 1 and 1000. The program should loop until the player decides he or she no longer wants to play.我用 java 编写了一个猜谜游戏来猜一个 1 到 1000 之间的数字。程序应该循环直到玩家决定他或她不再想玩。 I can not get it to loop however.但是,我无法让它循环。 If I take out the Scanner class at the bottom of the program it loops fine but I need the scanner class to complete the program as required in my class.如果我取出程序底部的 Scanner 类,它会很好地循环,但我需要扫描器类来完成我班上要求的程序。 If the user types "no" in the scanner class the while loop variable is set to false and the program terminates just as it should.如果用户在扫描器类中键入“no”,while 循环变量将设置为 false,程序将按原样终止。 But I cannot get it to loop if the user chooses "yes" and the while loop variable remains true.但是如果用户选择“是”并且 while 循环变量保持为真,我就无法让它循环。 After "yes" is typed the program just pauses and nothing happens, and the program doesn't end.输入“是”后,程序只是暂停,什么也没有发生,程序并没有结束。 Any help would be greatly appreciated.任何帮助将不胜感激。

import javax.swing.*;
import java.util.*;
public class RandomGuess3
{
   public static void main(String[] args)
   {
      String userGuess;
      String playAgain;
      int guessNum;
      int randomNum = (1+(int)(Math.random()*1000));
      int guessCount = 0;
      boolean correct = false;
      boolean startGame = true;
      boolean newGame = false;

         if(launchGame() == true)
            startGame = true;
         while(startGame == true)
         {
            do
            {
               userGuess = JOptionPane.showInputDialog(null,
                  "I'm thinking of a number between 1 and 1000."
                  +"\nEnter your guess:", "Guessing Game",
                  JOptionPane.QUESTION_MESSAGE);
               guessNum = Integer.parseInt(userGuess);
               if(guessNum != randomNum)
                  if(guessNum < randomNum)
                     JOptionPane.showMessageDialog(null, "Sorry you guessed too low");
                  else
                     JOptionPane.showMessageDialog(null, "Sorry you guessed too high");
               if(guessNum == randomNum)
                  correct = true;

                  ++guessCount;
            }while(guessNum != randomNum);

            if(correct == true)
                  JOptionPane.showMessageDialog(null, "CONGRATULATIONS!"
                     +"\nYou guessed correctly!"
                     +"\nAnd it only took "+guessCount+" tries.");

            Scanner input = new Scanner(System.in);      
            System.out.print("Want to play again? Enter yes or no >> ");
            playAgain = input.nextLine().toUpperCase();

            if(playAgain.equals("NO"))
               startGame = false;
            else
               startGame = true;
         }

   }
   public static boolean launchGame()
   {
      int letsPlay;
      boolean startGame;
      letsPlay = JOptionPane.showConfirmDialog(null,
         "Want to play a guessing game?");
      startGame = (letsPlay == JOptionPane.YES_OPTION);
      System.out.println("startGame is "+startGame);
      return startGame;
   }

}

You could take out the你可以取出

if(launchGame() == true)
   startGame = true;

entirely, maybe using this:完全,也许使用这个:

boolean startGame = launchGame();             
while(startGame)
             {
                do
                {
                   userGuess = JOptionPane.showInputDialog(null,
                      "I'm thinking of a number between 1 and 1000."
                      +"\nEnter your guess:", "Guessing Game",
                      JOptionPane.QUESTION_MESSAGE);
                   guessNum = Integer.parseInt(userGuess);

Another potential structural improvement could be using the natural if else structure of the game.另一个潜在的结构改进可能是使用游戏的自然 if else 结构。 "if the guess was wrong, tell them lower or higher, else tell them they won and offer to play again" is really what it seems the game should do. “如果猜错了,告诉他们更低或更高,否则告诉他们他们赢了并提供再次玩”似乎游戏应该做的事情。

Also, your correct boolean is sort of redundant, because to exit the loop, correct MUST be true, so you don't need to check again if correct.此外,您的正确布尔值有点多余,因为要退出循环,正确必须为真,因此您无需再次检查是否正确。

So:所以:

                  if(guessNum < randomNum)
                     JOptionPane.showMessageDialog(null, "Sorry you guessed too low");
                  else
                     JOptionPane.showMessageDialog(null, "Sorry you guessed too high");
                  ++guessCount;
            }while(guessNum != randomNum);

            JOptionPane.showMessageDialog(null, "CONGRATULATIONS!"
                     +"\nYou guessed correctly!"
                     +"\nAnd it only took "+guessCount+" tries.");

The final thing you MAY want to consider, and this is the most preference-based of all, is you may want to check yes or no differently.您可能要考虑的最后一件事,这是最基于偏好的,您可能想以不同的方式检查是或否。 Usually when I see these sorts of checks, I usually see people grab the first letter of the String response, and do a charAt(0), and if charAt(0) == 'y' or charAt(0) == 'Y', then play again, otherwise you have a no.通常当我看到这些类型的检查时,我通常会看到人们抓取 String 响应的第一个字母,然后执行 charAt(0),如果 charAt(0) == 'y' 或 charAt(0) == 'Y ',然后再玩一次,否则你就没有。 Some food for thought if you prefer that approach.如果您更喜欢这种方法,请深思熟虑。

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

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