boolean值不会WHILE循环java编程

时间:2015-06-28 22:37:53

标签: java

我有一个问题我还没有遇到过。我在java中编写了一个猜测游戏来猜测1到1000之间的数字。程序应该循环直到玩家决定他或她不再想玩。然而,我不能让它循环。如果我取出程序底部的Scanner类,它会很好地循环,但我需要扫描器类来完成我班级所需的程序。如果用户键入" no"在scanner类中,while循环变量设置为false,程序就像它应该的那样终止。但是如果用户选择"是"我就无法循环它。并且while循环变量保持为真。在"是"键入程序只是暂停,没有任何反应,程序不会结束。任何帮助将不胜感激。

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;
   }

}

1 个答案:

答案 0 :(得分:1)

你可以拿出

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

完全可能使用这个:

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);

另一个潜在的结构改进可能是使用游戏的自然if else结构。 “如果猜测是错误的,告诉他们更低或更高,否则告诉他们他们赢了并提供再次参加”真的是游戏应该做的事情。

另外,你的正确布尔值有点多余,因为要退出循环,必须正确,所以你不需要再次检查是否正确。

所以:

                  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.");

你可能想要考虑的最后一件事,这是最基于偏好的,你可能想要检查是或否。通常当我看到这些类型的检查时,我通常会看到人们抓住String响应的第一个字母,然后执行charAt(0),如果charAt(0)=='y'或charAt(0)=='Y ',然后再玩,否则你没有。如果你喜欢这种方法,可以考虑一些事情。