views:

104

answers:

3

Hey guys , thanx for the previous help Before I try to explain my problem, you guys need to know what the code is about. Its pretty much "To write a game in which a user is to guess a random number between 1 and 1000. The program should read a number from the keyboard, and print whether the guess was too high, too low or correct. When the user has guessed correctly, the program prints out the numbe of guesses made and time and the playername.When a game is started the program must print the entire high score list, sorted by the number of guesses in ascending"

The Issue is when playing the game and when you manage to get the correct answer it doesn't show the time but when the highscore is printed it shows there, Whats up with that because tried to change the boolean statement but that didn't see to work. Here is an illustration of the problem:

You guessed 2 times in 0 seconds.
Please enter your name.
gert
Want to go again?(y/n).....n
HighScore:
Tries        Time       Name
1             35        b
2             6         gert

SO basically I'm pretty stuck, I was hoping that u guys could give me some pointers or some kind of help so I could fix the problem,,,,, Any help is appreciated.... BTW this is my first program, basically still in the learning phase So take it easy with the comments. The code is provided below:

import java.util.*;
import java.util.Scanner.*;
import java.util.ArrayList.*;
import java.util.Collections.*;


public class Main {
private static void start() {


 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<Integer> score = new ArrayList<Integer>();
ArrayList<Long> tg = new ArrayList<Long>();
ArrayList<String> playern = new ArrayList<String>();



boolean a=false;

  do {
      a=false;
     int answer = (int) (Math.random() * 1000 + 1) ;
    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();
                        continue;



            }

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

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


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


    else if (guess == answer)
        {       
            //stop stop watch
            long endTime = System.currentTimeMillis();
            //calculate game time
            long gameTime = endTime - startTime;
                        gameTime = (gameTime/1000);
            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();

                          score.add(tries) ;
                          playern.add(name);
                          tg.add(gameTime);

                          for ( int g=0; g < score.size()-1; g++){
                              for ( int b=g+1; b < score.size(); b++){
                                  if (score.size()>1){
                                  if  (score.get (g) > score.get (b)){
                                    Collections.swap(score, g, b);
                                    Collections.swap(playern, g, b);
                                    Collections.swap(tg, g, b);

                                  }
                                  }
                                  if (score.get (g)==score.get(b) && tg.get (g) > tg.get(b))
                                  {
                                    Collections.swap(score, g, b);
                                    Collections.swap(playern, g, b);
                                    Collections.swap(tg, g, b);
                                  }
                              }

                          }



             boolean s = false  ; 
             while (s==false)
              {


                     System.out.print("Want to go again?(y/n).....");
                     currentGuess = input.nextLine();
                      if (currentGuess.matches("y"))
            {

                        System.out.println("HighScore:");
                        System.out.println("Tries\tTimentName");
                     for (int j=0; j<score.size(); j++){
                    System.out.println(score.get(j) +"\t"+tg.get(j)+ "\t"+playern.get(j));
                            }


                }

                     s=true;

            }

                     //if user doesn't want to play again

                             if (currentGuess.matches("n"))
                             { System.out.println("HighScore:");
                               System.out.println("Tries\tTime\t\tName");
                for (int j=0; j<score.size(); j++){
                System.out.println(score.get(j) +"\t"+tg.get(j)+ "\t"+playern.get(j));
                            }


                                System.out.println("Thanx For Playing.");
                a=true;

                                s=true;
                                 System.exit(0);  
                    } 
       }



        } while (guess != answer);

  }while(a==false);

}

public static void main(String[] args) {
    Main.start();
    }
}
+7  A: 

first you divide the time here

long gameTime = endTime - startTime;
gameTime = (gameTime/1000);

and then again here

System.out.println("You guessed " + tries + " times in " + (int)(gameTime/1000) + " seconds.");

no wonder why you get 0 :). so, don't divide again, just put gameTime

and then when you add to the tg Array List you add tg.add(gameTime); which is the correct time, that's why when you list the Highscores it works.

undertakeror
+1  A: 

A couple of things here.

First, there is no Timer being used here. You are timing something in terms of actual time, but in Java, a Timer (such as java.util.Timer) is an object that schedules a repeated task.

Second, you should try to break up your code into smaller methods. These long, nested do/while loops are very hard to read because you can't see the whole loop without scrolling. Try keeping your methods shorter, and use multiple methods.

But, to answer your question about the time, you are dividing the time by 1000 twice when you try to display it. Your display line is

System.out.println("You guessed " + tries + " times in " + (int)(gameTime/1000) + " seconds.");

But you have already divided gameTime above

long gameTime = endTime - startTime;
gameTime = (gameTime/1000);

So in the display, you're not actually modifying gameTime again (which is why it stores as the proper value), but you are displaying gameTime/1000 (even though it has already been divided by 1000 once).

Jeff Storey
A: 

It is also a good thing for a starting developer to follow the simple indentation rules you've (hopefully) learned.

Also you should't write something like while(a==false); if a is a boolean just write while(a).

In this case you also do not have to use a do - while loop. A while loop will be more suited.

Dave