开发者

Issues with Number of guesses doesn't update for a new try and the code causses errors when typing the number 1005,1006?

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"

Everything works, but the problem is when I try typing the code with 1005 and 1006 it causes errors. and the number of guesses doesn't update for a new try! For the number of guesses i tried to add a boolean statement but that didn't seem to work and for the numbers i couldn't find why it causes errors. 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 below:

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


public class Game {
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();


   }

  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 "+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\t开发者_开发百科Time\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) {

    Game.start();
    }
}


You need continue the two places you reject the input.


The question stated "and the number of guesses doesn't update for a new try", which i interpreted as you wanted 1000+ answers to increment the number of tries.

New Answer: you aren't resetting tries inside the initial game loop.

    do
    {
        a = false;

        int answer = (int) (Math.random() * 1000 + 1); 
        tries = 0;  // <------------- missing this line

        System.out.println(" Welcome to Guessing Game ");
        System.out.print("Please enter a number between 1 and 1000 : ");

I'm not sure what you mean about "errors" above 1005, the game seems to work just fine to me.

Aside from the fact that you're outputting the answer on every guess:

                System.out.println("too high " + answer); // <---------- why are you outputting the answer?!?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜