2015-07-02 47 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 
}