Java:达到一定值后重新启动程序

时间:2014-05-06 08:53:41

标签: java loops boolean

我制作的游戏允许用户猜测随机数。如果用户猜测正确,并且他/她决定再次播放,则必须生成新号码。我假设如果我设置布尔值"重启"到"真"在两个开头(虽然很快就把它弄错),然后在"重新开始"之后将其设置回真。再次成为现实,然后程序将重新开始......它没有。你看到我做错了什么吗?感谢。

  import java.util.*;
  import javax.swing.*;
  import java.util.Scanner;

  public class RandomGuess2
  {
  public static void main(String[] args)
  {
  Scanner input = new Scanner(System.in);

  String userNum;
  int userInput, numInt, genNum, amount = 0;
  int repeatPlay = 0;
  int x = 1;
  boolean numTruth, restart;
  boolean repeatIsYes = false;

  restart = true;
  userInput = JOptionPane.showConfirmDialog(null, "Would you like to try and guess the magic    
  number?", "Guessing Game", JOptionPane.YES_NO_OPTION);

  genNum = (1 + (int)(Math.random() * 1000));


  do
    if((numTruth = (userInput == JOptionPane.YES_OPTION)) || (repeatIsYes = (userInput == 
    JOptionPane.YES_OPTION)))
    restart = false;
    {
    userNum = JOptionPane.showInputDialog(null,"Enter the number you're thinking between 1 & 
    1000: ");
    numInt = Integer.parseInt(userNum);

    if((numInt > 1000) || (numInt < 1)) {
     JOptionPane.showMessageDialog(null, "Incorrect entry");
     repeatIsYes = true;
     }
     else if(numInt > genNum) {
     repeatIsYes = true;
     repeatPlay = JOptionPane.showConfirmDialog(null, "You guessed too high!" 
      + "\nWould you like to guess again?" + genNum);
     }
     else if(numInt < genNum) {
     repeatIsYes = true;
     repeatPlay = JOptionPane.showConfirmDialog(null, "You guessed too low!" 
      + "\nWould you like to guess again?" + genNum);
     }
     else if(numInt == genNum) {
     repeatIsYes = false;
     repeatPlay = JOptionPane.showConfirmDialog(null, "How did you do that? You guessed  
      + correctly!\nWould you like to guess again?" + genNum);
     if(restart = (repeatPlay == JOptionPane.YES_OPTION))
     {restart = true;}
     }

     if(repeatIsYes = (repeatPlay == JOptionPane.YES_OPTION)) {
     numTruth = true;
     repeatIsYes = true;
     }
     else {
     numTruth = false;
     repeatIsYes = false;
     JOptionPane.showMessageDialog(null, "Goodbye! \nYou played " + amount + " times.");
     break;
     }
    }
    else {
    numTruth = false;
    repeatIsYes = false;
    JOptionPane.showMessageDialog(null, "Goodbye!");
    break;
    }
  while(numTruth = true);      
  }}

1 个答案:

答案 0 :(得分:1)

从不真正使用restart,即你只设置它。要重新启动应用程序,请在循环中的适当位置检查restart的值。

我没有时间彻底阅读您的代码(由于格式和结构,这很难读取,但是一个简单的解决方案就是使用两个循环。

这里有一些伪代码:

while( playAnotherGame) {  //this could be your "restart" flag
  boolean gameIsRunning = true;

  while( gameIsRunning ) {
    //ask for user input
    //check guesses
    if( guessedCorrectly ) {
      gameIsRunning  = false; //game is finished

      //display result
      //ask for user input: restart or quit
      if( quit ) {
        playAnotherGame = false;
      }       
    }
    else {
      //ask for user input: guess again, new game or quit
      //handle user input 
    }
  }
}

顺便说一句,像这样的结构容易出错且难以阅读:

//you set and check repeatIsYes at the same time
if(repeatIsYes = (repeatPlay == JOptionPane.YES_OPTION)) {
  numTruth = true;
  repeatIsYes = true; //this is already true, see the if-condition
}