java - Problems with Making a Highscore and Loops and Overall Issues -


hey guys , thanx previus help..... time it's homework meaning want me make highscore shows playername,score (tries took) , time. wants:
write game in user guess random number between 1 , 1000. program should read number keyboard, , print whether guess high, low or correct. when user has guessed correctly, program prints out numbe of guesses made , time , playername.when game started program must print entire high score list, sorted number of guesses in ascending order. note: list must maintained long game-object alive! example without time;

guess high! > 813 guess high! > 811 **** correct! **** guessed correct number in 11 guesses please enter name: > niklas want play again?(y/n)  >y current highscore list: name guesses niklas 11 

my questions are; code provided below enough mainten these requirements, if not should add because don't know do? , please consider i'm still in learning phase take easy comments :) here code:

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 = ("y") ;  scanner input = new scanner (system.in);   arraylist<score> scores = new arraylist<score>();       system.out.println( " welcome guessing game " ) ;     system.out.print("please enter number between 1 , 1000 : ");                     currentguess = input.nextline();       long starttime = system.currenttimemillis();                  {                  if (currentguess.equalsignorecase(quit))   {    system.out.println("leaving soon?");    system.exit(0);   }                 try    {    guess = integer.parseint(currentguess);         } catch (numberformatexception nfe)                          {    system.out.println(" dude can read, digits ");                         currentguess = input.nextline();      }    if (guess < 1 || guess > 1000)   {    system.out.println("stupid guess 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, job!");                          system.out.println("you guessed " + tries + " times in " + (int)(gametime/1000) + " seconds.");                         system.out.println("please enter name.");             name = input.nextline();                           //create score object    score currentscore = new score(tries, gametime, name);    //add score arraylist    scores.add(currentscore);                        scanner playgame = new scanner(system.in);                      system.out.print("want go again?(y/n).....");                       if (currentguess.equalsignorecase(y))    {                      system.out.println("guess \t time in miliseconds \t name");     //print out high score list     (int = 0;i < scores.size(); i++)     {     system.out.println(scores.get(i));     }      = playgame.nextline();                                  main.start();    }                //if user doesn't want play again    if (currentguess.equalsignorecase(n))    {     system.out.println("guess \t time in miliseconds \t name");     //print out high score list     (int = 0;i < scores.size(); i++)     {     system.out.println(scores.get(i));     }                                 system.out.println("thanx playing.");     system.exit(0);                     }           }          } while (guess != answer);    }  public static void main(string[] args) {    arraylist<score> scores = new arraylist<score>();     main.start();     } } 

some remarks:

  • in class score constructor score() unnecessary since don't use it.
  • you have typo in tostring method of class. should tostring, starting lowercase letter.
  • you can make 3 fields of score class final. way cannot changed after constructor has finished.
  • you have many unused local variables. can remove them, makes code clearer.

back original question regarding high score table. once had same task , made highscore own class. looked approximately this:

public class highscore {    private final int maxentries;   private list<score> scores = new arraylist<score>();    /**    * creates new highscore table keeps <i>maxentries</i> best entries.    */   public highscore(int maxentries) {     this.maxentries = maxentries;   }    /**    * adds entry highscore table if enough.    *    * @return {@code true} if score has been added, {@code false} otherwise.    */   public boolean add(score score) {     // todo: add entry     // todo: make sure entries sorted correctly; hints: collection.sort, comparator     // todo: throw out worst entry if there more maxentries   }    /**    * returns highscore table.    *    * @return highscore entries in correct order. first entry best one.    */   public list<score> gettable() {     return collections.unmodifiablelist(scores);   }  } 

when have such class, can write tests make sure works correctly. example:

@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()); } 

testing great tool make sure code works basic cases. keyword further search "junit".


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -