Java 高分、循环和总体问题的问题

Java 高分、循环和总体问题的问题,java,Java,嘿,伙计们,谢谢普雷维乌斯的帮助。。。。。 这一次是家庭作业,意味着他们希望我取得高分,以显示玩家姓名、分数(尝试次数)和时间。 基本上它想要: 编写一个游戏,其中用户要猜测1到1000之间的随机数。程序应该从键盘上读取一个数字,然后打印猜测是否过高、过低或正确。当用户猜对了,程序会打印出猜对的次数、时间和玩家姓名 已启动的程序必须打印整个高分列表,按猜测次数升序排序。注意:只要游戏对象处于活动状态,就必须维护此列表! 没有时间的榜样 Guess is too high! > 813 Gu

嘿,伙计们,谢谢普雷维乌斯的帮助。。。。。 这一次是家庭作业,意味着他们希望我取得高分,以显示玩家姓名、分数(尝试次数)和时间。 基本上它想要:
编写一个游戏,其中用户要猜测1到1000之间的随机数。程序应该从键盘上读取一个数字,然后打印猜测是否过高、过低或正确。当用户猜对了,程序会打印出猜对的次数、时间和玩家姓名 已启动的程序必须打印整个高分列表,按猜测次数升序排序。注意:只要游戏对象处于活动状态,就必须维护此列表! 没有时间的榜样

Guess is too high!
> 813
Guess is too high!
> 811
**** CORRECT!
**** You guessed the correct number in 11 guesses
Please enter you name:
> niklas
Do you want to play again?(y/n)
 >y
Current highscore list:
Name Guesses
niklas 11
我的问题是,;下面提供的代码是否足以维护这些需求,如果没有,我应该添加什么,因为我真的不知道该怎么做?请考虑我仍然是在学习阶段,所以轻松的评论: 代码如下:

package testa;
import java.util.*;
import java.util.Scanner.*;
import java.util.ArrayList.*;


public class Main {
private static class Score {

 int playerScore = 0;
 double playerTime = 0;
 String playerName;

 public Score ()
 {
 }

 public Score (int playerScore, double playerTime, String playerName)
 {
  this.playerScore = playerScore;
  this.playerTime = playerTime;
  this.playerName = playerName;
 }

 public String ToString()
 {
  String scoreList = (playerScore + "\t\t" + playerTime + "\t\t" + playerName);

  return scoreList;
 }
}

private static void start() {

 int answer = (int) (Math.random() * 1000 + 1) ; 
 int tries = 0 ;

 int guess = -1;
 String name ;
 String quit = "quit";
 String y = "yes";
 String n = "no";
 String currentGuess;

 String another = ("y") ;
 Scanner input = new Scanner (System.in);

 ArrayList<Score> scores = new ArrayList<Score>();


    System.out.println( " Welcome to Guessing Game " ) ;
    System.out.print("Please enter a number between 1 and 1000 : ");
                    currentGuess = input.nextLine();
      long startTime = System.currentTimeMillis();





      do
      {


               if (currentGuess.equalsIgnoreCase(quit))
  {
   System.out.println("Leaving Us So Soon?");
   System.exit(0);
  }

               try    {
   guess = Integer.parseInt(currentGuess);
        } catch (NumberFormatException nfe) 
                        {
   System.out.println(" Dude Can You Read, Only Digits ");
                        currentGuess = input.nextLine();


   }

  if (guess < 1 || guess > 1000)
  {
   System.out.println("Stupid Guess I Wont Count That.");
                        currentGuess = input.nextLine();

  }
       if (guess < answer )
          {
   System.out.println("too low");
   currentGuess = input.nextLine();
                        tries++;
  }


    else if(guess  > answer )
  {
   System.out.println("too high");
   currentGuess = input.nextLine();
                        tries++;
  }


    else if (guess == answer)
  {       
   //stop stop watch
   long endTime = System.currentTimeMillis();
   //calculate game time
   long gameTime = endTime - startTime;
   System.out.println("You Rock Dude, Good Job!");

                        System.out.println("You guessed " + tries + " times in " + (int)(gameTime/1000) + " seconds.");
                        System.out.println("Please enter your name.");
            name = input.nextLine();
                          //create score object
   Score currentScore = new Score(tries, gameTime, name);
   //add score to arrayList
   scores.add(currentScore);


                     Scanner playGame = new Scanner(System.in);
                     System.out.print("Want to go again?(y/n).....");
                      if (currentGuess.equalsIgnoreCase(y))
   {
                     System.out.println("Guess \t Time in miliseconds \t Name");
    //print out high score list
    for (int i = 0;i < scores.size(); i++)
    {
    System.out.println(scores.get(i));
    }
     another = playGame.nextLine();
                                 Main.start();
   }
               //if user doesn't want to play again
   if (currentGuess.equalsIgnoreCase(n))
   {
    System.out.println("Guess \t Time in miliseconds \t Name");
    //print out high score list
    for (int i = 0;i < scores.size(); i++)
    {
    System.out.println(scores.get(i));
    }
                                System.out.println("Thanx For Playing.");
    System.exit(0);
                    }
          }

        } while (guess != answer);



}

public static void main(String[] args) {
   ArrayList<Score> scores = new ArrayList<Score>();
    Main.start();
    }
}
封装测试;
导入java.util.*;
导入java.util.Scanner.*;
导入java.util.ArrayList.*;
公共班机{
私有静态类分数{
int playerScore=0;
双播放时间=0;
弦乐演奏者姓名;
公众分数()
{
}
公开分数(整数playerScore、双人playerTime、弦乐playerName)
{
this.playerScore=playerScore;
this.playerTime=playerTime;
this.playerName=playerName;
}
公共字符串ToString()
{
字符串记分表=(playerScore+“\t\t”+playerTime+“\t\t”+playerName);
返回记分表;
}
}
私有静态void start(){
int答案=(int)(Math.random()*1000+1);
int=0;
int guess=-1;
字符串名;
String quit=“退出”;
字符串y=“是”;
字符串n=“否”;
字符串猜测;
字符串另一个=(“y”);
扫描仪输入=新扫描仪(System.in);
ArrayList分数=新建ArrayList();
System.out.println(“欢迎来到猜谜游戏”);
System.out.print(“请输入一个介于1和1000之间的数字:”);
currentGuess=input.nextLine();
long startTime=System.currentTimeMillis();
做
{
if(currentGuess.equalsIgnoreCase(退出))
{
System.out.println(“这么快就离开我们了?”;
系统出口(0);
}
试一试{
guess=Integer.parseInt(currentGuess);
}捕获(NumberFormatException nfe)
{
System.out.println(“都德,你能读,只有数字”);
currentGuess=input.nextLine();
}
如果(猜测<1 | |猜测>1000)
{
System.out.println(“愚蠢的猜测,我不会数一数。”);
currentGuess=input.nextLine();
}
如果(猜测<回答)
{
System.out.println(“过低”);
currentGuess=input.nextLine();
尝试++;
}
else if(猜测>回答)
{
System.out.println(“过高”);
currentGuess=input.nextLine();
尝试++;
}
else if(猜测==答案)
{       
//秒表
long-endTime=System.currentTimeMillis();
//计算比赛时间
长游戏时间=结束时间-开始时间;
System.out.println(“你这个摇滚小子,干得好!”);
println(“你在“+(int)(游戏时间/1000)+“秒”中猜到了“+尝试次数+”);
System.out.println(“请输入您的姓名”);
name=input.nextLine();
//创建分数对象
当前分数=新分数(尝试次数、游戏时间、名称);
//将分数添加到arrayList
分数。添加(当前分数);
扫描器游戏=新扫描器(System.in);
System.out.print(“想再去吗?(y/n)…”;
if(currentGuess.equalsIgnoreCase(y))
{
System.out.println(“猜测\t时间单位为毫秒\t名称”);
//打印出高分表
对于(int i=0;i
一些备注:

  • 在类
    Score
    中,构造函数
    Score()
    是不必要的,因为您不使用它
  • 该类的
    ToString
    方法中有一个输入错误。它应该是
    toString
    ,以小写字母开头
  • 您可以将
    分数的所有三个字段设置为
    最终
    。这样,在构造函数完成后就不能更改它们
  • 您有许多未使用的局部变量。您可以删除它们,这使代码更清晰
回到关于高分表的原始问题。我曾经不得不做同样的任务,我把高分作为自己的课程。大概是这样的:

public class HighScore {

  private final int maxEntries;
  private List<Score> scores = new ArrayList<Score>();

  /**
   * Creates a new highscore table that keeps only <i>maxEntries</i> best entries.
   */
  public HighScore(int maxEntries) {
    this.maxEntries = maxEntries;
  }

  /**
   * Adds an entry to the highscore table if it is good enough.
   *
   * @return {@code true} if the score has been added, {@code false} otherwise.
   */
  public boolean add(Score score) {
    // TODO: add the entry
    // TODO: make sure the entries are sorted correctly; some hints: Collection.sort, Comparator
    // TODO: throw out the worst entry if there are more than maxEntries
  }

  /**
   * Returns the highscore table.
   *
   * @return The highscore entries in the correct order. The first entry is the best one.
   */
  public List<Score> getTable() {
    return Collections.unmodifiableList(scores);
  }

}

测试是一个很好的工具,可以确保您的代码适用于基本情况。进一步搜索的一个好关键字是“JUnit”。

请再打几个问号……你怎么能不确定它是否符合要求?只要试着去做你的要求,如果你的程序不能得到预期的结果,那么你需要做更多的工作。你的“问题”毫无意义。相反,告诉我们一些东西是如何不按照要求工作的,你的想法和修复它的尝试,然后也许你可以得到一些真正的帮助;这就是为什么我问你们,因为我觉得
@Test
public void testEmptyTable() {
  HighScore highscore = new HighScore(5);
  assertTrue(highscore.getTable().isEmpty());
  assertTrue(highscore.add(new Score(1, 0.5, "Caesar Primus")));
  assertFalse(highscore.getTable().isEmpty());
}