簡體   English   中英

猜謎游戲 Java JCreator

[英]Guessing Game Java JCreator

  1. 我必須使用java.utiljava.util的 random 類創建一個程序,以生成 1 到 100 之間的數字。
  2. 必須給用戶最多 6 次猜測數字的機會,如果他們在第 6 次嘗試中未能正確猜測,則應顯示目標數字。 該程序需要向用戶提供反饋,指出他們的猜測是過高還是過低。 如果猜對了,程序會說“恭喜你,你猜對了”。
  3. 用戶應該在開始會話時提供他們的名字,這應該被存儲起來並在后續的記錄生成中使用。
  4. 該程序應為用戶創建一個會話,並應允許他們在該會話中擁有任意數量的訪問。 每次運行時,程序都應該生成一個新的隨機數,並為用戶提供最多 6 次猜測的機會。 當目標號碼被正確猜到或已經嘗試了 6 次時,用戶應該可以選擇退出或進行另一場比賽/開始。

這是我到目前為止:

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 部分嗎?

我建議通過 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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM