猜猜游戏Java JCreator

时间:2015-07-02 20:56:46

标签: java

  1. 我必须使用包java.util中的随机类创建一个程序,以生成1到100之间的数字。
  2. 用户必须最多6次尝试猜测一个数字,如果他们在第6次尝试时未能正确猜测,则应显示目标数字。该程序需要向反馈用户提供指示他们的猜测是否过高或过低的信息。如果猜测正确,程序会说“恭喜,你的猜测是正确的。”
  3. 用户应在开始会话时提供其名称,并应存储并在后续记录生成中使用。
  4. 程序应该为用户创建一个会话,并且应该允许他们在该会话中拥有尽可能多的内容。程序应该每次都生成一个新的随机数,并为用户提供最多6次猜测。当目标号码被正确猜测或已经尝试了6次时,用户应该可以选择退出或进行另一场比赛。
  5. 这是我到目前为止所做的:

    import java.util.Random;
    import javax.swing.JOptionPane;
    
    public class GuessANumber3 {
        public static int TARGET = 0;
    
        public static void main(String[] args) {
            Random R = new Random();
            TARGET = R.nextInt(100);
            String guessString;
            int guess;
            int count = 0;
            int bestScore = 0;
            System.out.println(TARGET);
            do {
                // read in a number from user as a string
                guessString = JOptionPane.showInputDialog("Enter first integer");
                // convert number from type String to type int
                guess = Integer.parseInt(guessString);
                count++;
                if (guess > TARGET) {
                    JOptionPane.showMessageDialog(null, "Your guess is too high", "Hint",
                                    JOptionPane.PLAIN_MESSAGE);
                } else {
                    if (guess < TARGET) {
                        JOptionPane.showMessageDialog(null, "Your guess is too low", "Hint",
                                        JOptionPane.PLAIN_MESSAGE);
                    }
                }
                System.out.println(count);
                if (count == 6)
                    break;
    
            } while (guess != TARGET);
            if (guess == TARGET)
                JOptionPane.showMessageDialog(null, "You found it with " + count + "guesses.",
                                "Congratulations!", JOptionPane.PLAIN_MESSAGE);
            else
                JOptionPane.showMessageDialog(null, "You have reached the maximum attempts in this go",
                                "Attention", JOptionPane.PLAIN_MESSAGE);
    
            if (count < bestScore)
                bestScore = count;
        }
    }
    

    任何人都可以帮我解决第3和第4部分吗?

1 个答案:

答案 0 :(得分:2)

我建议通过oop设计解决问题。根据您的要求,

  • 用户 - 有一个名字
  • 会话 - 会话适用于用户
  • 尝试 - 在用户猜测时在游戏中完成
  • 游戏 - 最大尝试次数为6

所以在伪代码中

   class User
   {
      String name;
   }

   class Session
   {
      User user;
      Game currentGame;

      void startNextGame()
      {
          //create game, when game end, ask to continue
      }
   }

   class Game
   {
      int ties = 6;
      int number;
      Game()
      {
          Random random = new Random();
          number = random.nextInt();
      }

      void play()
      {
         for( int i = 0; i < tries; ++i )                                                               
         {
             Attempt attempt = new Attempt( number );
             attempt.try();
             if( attempt.guessed() )
             {
                //Show guessed
                return;
             }                                                                                                                                                                               
         }
         //show unguessed   
      }
   }

   class Attempt()
   {
      int expectedNumber;
      Attempt( int number )
      {
         expectedNumber = number;
      }

      void try()
      {
         //get guess
      }

      boolean guessed()
      {
        //return result of try
      }
   }

void main()
{
     //getUser
     User user;
     //if I have session for user, getSession, if not create and store ex. map
    //start next game in session
}
相关问题