2015-07-02 48 views
-1
  1. 我有使用隨機類中包java.util,以產生一個數字來創建程序1和100
  2. 之間的用戶已經被給予6次試圖猜測的數的最大如果他們在第6次嘗試中未能正確猜測,則應顯示目標號碼。該計劃需要向反饋用戶提供指示他們的猜測是否過高或過低。如果猜測是正確的,程序會說「恭喜,你的猜測是正確的。」
  3. 用戶應該在開始會話時提供他們的名字,這應該被存儲並用於隨後的記錄生成。
  4. 程序應該爲用戶創建一個會話,並且應該允許他們在該會話中擁有儘可能多的內容。在每一個程序中,程序都應該生成一個新的隨機數,併爲用戶提供多達6次的猜測。當目標號碼被正確猜測或試圖做出6次嘗試時,應該讓用戶選擇退出或進行其他遊戲/去。

這是我到目前爲止有:猜測遊戲的Java JCreator的

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?

+0

你有具體問題是否在3和4? – Tom

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