【问题标题】:Java: Restarting program after achieving certain valueJava:达到一定值后重新启动程序
【发布时间】:2014-05-06 08:53:41
【问题描述】:

我正在制作一个允许用户猜测随机数的游戏。如果用户猜对了,并且他/她决定再玩一次,则必须生成一个新号码。我假设如果我在开始时都将布尔值“restart”设置为“true”(此后不久将其设置为false),然后在“restart”再次变为true后将其设置回true,那么程序将重新开始。 ..它没。你看到我做错了吗?谢谢。

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

【问题讨论】:

    标签: java loops boolean


    【解决方案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
    }
    

    【讨论】:

    • 感谢您的帮助。从现在开始,我将寻找资源来澄清我的代码,以加强格式和结构。
    猜你喜欢
    • 1970-01-01
    • 2014-09-14
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多