//****************************//
//*****DO NOT TOUCH THESE*****//
//****************************//
import java.util.*;
import java.io.*;

public class NumberGuessingGame{

  public static int num;    //variable to hold randomly generated number
  public static int guess;  //variable to hold user inputs
  public static String ans; //variable to hold user input for y/n
  public static int guessnumber = 7; //variable to hold number of guesses
  public static int correctguess; //variable to hold number of correct guesses

  //the main method
  public static void main(String[] args) throws IOException{

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

    //introduction
    System.out.println("Welcome to the Number Guessing Game!");

    //loop to allow the user to play again
    ans = "y";
    while(ans.equals("y")){
      numberGenerator();
      System.out.println("\nI have generated a random number between 1 and 100.  Please guess what the number is.");}

      //TASK 1: limit the user to only 7 guesses
      guess = Integer.parseInt(br.readLine());

      //TASK 2 & 3: write in the conditions for when the user's guess is too high, too low, or correct
      //if the guess is correct, the user should not be able to keep guessing
      if(guess > num){
        System.out.println("Your guess is too low.");
      }
      else if(guess < num){
        System.out.println("Your guess is too high.");
      }
      else if(guess < 0){
        System.out.println("You have entered a number below 0. Would you like to try again? (y/n)");
      }
      else if(guess < 100){
        System.out.println("You have entered a number above 100. Would you like to try again? (y/n)");
      }
      else if(guess == num){
        System.out.println("CONGRATULATIONS! You have guessed the number correctly! Would you like to play again? (y/n)");
        correctguess++;}

        if(guessnumber == 6){
          System.out.println("You have 6 guesses left.");
        }
        else if(guessnumber == 5){
          System.out.println("You have 5 guesses left.");
        }
        else if(guessnumber == 4){
          System.out.println("You have 4 guesses left.");
        }
        else if(guessnumber == 3){
          System.out.println("You have 3 guesses left.");
        }
        else if(guessnumber == 2){
          System.out.println("You have 2 guesses left.");
        }
        else if(guessnumber == 1){
          System.out.println("You have 1 guess left.");
        }
        else if(guessnumber == 0){
          System.out.println("You have ran out of guesses! Would you like to play again? (y/n)");}


      //TASK 4: if the user has used up the 7 guesses, let the user know that they have run out of guesses
      //they should then have the option of playing again

      System.out.println("Play again? (y/n)");
      ans = br.readLine();

      if(ans.equals("n")){
        System.out.println("Thank you for playing!");
        System.out.println("You have gotten" + correctguess + "guessess right");


        if(guessnumber == 7){
          System.out.println("You have ran out of guesses! Would you like to play again? (y/n)");
      }
      else if(ans.equals("y")){
      }
      else{
        System.out.println("Please enter y/n");
      }
     }

    //COMPLETE THE REMAINING TASKS (5-8)

  }


  //the numberGenerator method
  //this method generates the random number
public static void numberGenerator(){

    // ********* DO NOT TOUCH ************//
    Random generator = new Random();
    num = generator.nextInt(100) + 1;
    // ***********************************//

}}

In this code I am working on for my class, I am creating a number guessing game. I have completed all of it, but when I compile it, it creates an infinite loop of Line 27. I don't know why, could someone please help?

Edited 3 Years Ago by bob.henry.1884: I fixed my other error and have gotten a new one.

It's been a while since I've looked at Java, so, forgive me if this does not work. In brief, what you're trying to do is delcare a method within a method - This is not allowed hence the error you're getting.

I have fixed the error, however, since you're declaring "NumberGuessingGame" as a public class, it therefore should exist in it's own file.

EDIT: I have also fixed the loop error (Yes, I'm in that good of a mood!!)

//****************************//
//*****DO NOT TOUCH THESE*****//
//****************************//
import java.util.*;
import java.io.*;

class NumberGuessingGame{

  public static int num;    //variable to hold randomly generated number
  public static int guess;  //variable to hold user inputs
  public static String ans; //variable to hold user input for y/n
  public static int guessnumber = 7; //variable to hold number of guesses
  public static int correctguess; //variable to hold number of correct guesses


  //the main method
  public static void main(String[] args) throws IOException{

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

    //introduction
    System.out.println("Welcome to the Number Guessing Game!");

    //loop to allow the user to play again
    ans = "y";
    do{

        numberGenerator();
        System.out.println("\nI have generated a random number between 1 and 100.  Please guess what the number is.");
        guess = Integer.parseInt(br.readLine());

        //TASK 2 & 3: write in the conditions for when the user's guess is too high, too low, or correct
              //if the guess is correct, the user should not be able to keep guessing
              if(guess > num){
                System.out.println("Your guess is too low.");
              }
              else if(guess < num){
                System.out.println("Your guess is too high.");
              }
              else if(guess < 0){
                System.out.println("You have entered a number below 0. Would you like to try again? (y/n)");
              }
              else if(guess < 100){
                System.out.println("You have entered a number above 100. Would you like to try again? (y/n)");
              }
              else if(guess == num){
                System.out.println("CONGRATULATIONS! You have guessed the number correctly! Would you like to play again? (y/n)");
                correctguess++;}

                if(guessnumber == 6){
                  System.out.println("You have 6 guesses left.");
                }
                else if(guessnumber == 5){
                  System.out.println("You have 5 guesses left.");
                }
                else if(guessnumber == 4){
                  System.out.println("You have 4 guesses left.");
                }
                else if(guessnumber == 3){
                  System.out.println("You have 3 guesses left.");
                }
                else if(guessnumber == 2){
                  System.out.println("You have 2 guesses left.");
                }
                else if(guessnumber == 1){
                  System.out.println("You have 1 guess left.");
                }
                else if(guessnumber == 0){
                  System.out.println("You have ran out of guesses! Would you like to play again? (y/n)");}


              //TASK 4: if the user has used up the 7 guesses, let the user know that they have run out of guesses
              //they should then have the option of playing again

              System.out.println("Play again? (y/n)");
              ans = br.readLine();

              if(ans.equals("n")){
                System.out.println("Thank you for playing!");
                System.out.println("You have gotten" + correctguess + "guessess right");


                if(guessnumber == 7){
                  System.out.println("You have ran out of guesses! Would you like to play again? (y/n)");
              }
              else if(ans.equals("y")){
              }
              else{
                System.out.println("Please enter y/n");
              }
             }

    }while(ans != "y");
}

public static void numberGenerator(){

    // ********* DO NOT TOUCH ************//
    Random generator = new Random();
    num = generator.nextInt(100) + 1;
    // ***********************************//
}
}
  //the numberGenerator method
  //this method generates the random number

Test here:

http://ideone.com/q4kgsX

The code was written badly, and, is complicated to read and understand. You should re-write this, changing the logic of the program and the structure.

Hope it helps you.

Edited 3 Years Ago by phorce

bob.henry: The first thing to learn here is "never just copy code that someone else has given you". No matter how well-intended they may be, and no matter how good the code is, you won't learn much.
Maybe phorce could use his/her time now to help you learn how to debug a loop - that way you will (a) gain an essential skill and (b) fix your problem.

In the meantime - here's your lines 24-27 correctly layed out...

while(ans.equals("y")){
   numberGenerator();
   System.out.println("\nI (etc etc)");
}

Can you see theproblem there?

ps phorce: "The code was written badly, and, is complicated to read and understand. You should re-write this, changing the logic of the program and the structure." Do you really think that's helpful? Just telling someone their code is bad and telling them to change it is demoralising, maybe insulting, and gives zero help. If you want to make comments like that then explain how and why it's bad, and how to change that to make it better (maybe with a short example - not a complete re-write). Please remember we're not here to do people's homework, we're here to help them become better programmers.

Edited 3 Years Ago by JamesCherrill

@JamesCherrill - My bad. I was in a rush and therefore wanted to fix the problem over offering any tits or solutions how to fix the above code.

There are a lot of errors and syntax flaws within the submitted code and in no what what I typed was meant in an insulting way, my apologisies if this come across like so.

The code that I gave, should, no longer give you an infinite loop (From the last time I compiled it).

You should therefore break the code up. For example:

 if(guess > num){
                System.out.println("Your guess is too low.");
              }
              else if(guess < num){
                System.out.println("Your guess is too high.");
              }
              else if(guess < 0){
                System.out.println("You have entered a number below 0. Would you like to try again? (y/n)");
              }
              else if(guess < 100){
                System.out.println("You have entered a number above 100. Would you like to try again? (y/n)");
              }

You do not need so many else if statements, since, the output for each one of these is the same? If the output was different then I could understand why you would need more than one statement, however, 1 function which returns a Boolean depending on whether the guess is right or wrong SHOULD be enough. I.e.

if(numberGuessed)
{
   // this is true;
}else{
   // this is false;
}

Please feel free to post back if there are any more suggestions you would like making.

Can you see how the } on your line 27 defines the end of the while loop?
I assume you intended to have all the following code inside the loop, so this } is obviously in the wrong place (far too early).
Indenting your code correctly and placing all the } on their own lines will help you see the structure of the code, especially where loops etc finish. When you do that you should be able to see where your } should be.

@bob - You need to be able to terminate the program if the user enters "n" after the first guess.

Therefore just after line 80 inside this program you should enter: System.exit(0); to stop the program from repeating itself and therefore exit from the loop.

@bob - What version of Java are you running? On my machine, it runs completely fine..

@Bob mhm weird. Post the code that you're currently trying to run and I will take a look at it.

//****************************//
//*****DO NOT TOUCH THESE*****//
//****************************//
import java.util.*;
import java.io.*;

class NumberGuessingGame{

  public static int num;    //variable to hold randomly generated number
  public static int guess;  //variable to hold user inputs
  public static String ans; //variable to hold user input for y/n
  public static int guessnumber = 7; //variable to hold number of guesses
  public static int correctguess; //variable to hold number of correct guesses

  //the main method
  public static void main(String[] args) throws IOException{

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

    //introduction
    System.out.println("Welcome to the Number Guessing Game!");

    //loop to allow the user to play again
    ans = "y";
    while(ans.equals("y")){
      numberGenerator();
      System.out.println("\nI have generated a random number between 1 and 100.  Please guess what the number is.");}

      //TASK 1: limit the user to only 7 guesses
      guess = Integer.parseInt(br.readLine());

      //TASK 2 & 3: write in the conditions for when the user's guess is too high, too low, or correct
      //if the guess is correct, the user should not be able to keep guessing
      if(guess > num){
        System.out.println("Your guess is too low.");
      }
      else if(guess < num){
        System.out.println("Your guess is too high.");
      }
      else if(guess < 0){
        System.out.println("You have entered a number below 0. Would you like to try again? (y/n)");
      }
      else if(guess < 100){
        System.out.println("You have entered a number above 100. Would you like to try again? (y/n)");
      }
      else if(guess == num){
        System.out.println("CONGRATULATIONS! You have guessed the number correctly! Would you like to play again? (y/n)");
        correctguess++;}

        if(guessnumber == 6){
          System.out.println("You have 6 guesses left.");
        }
        else if(guessnumber == 5){
          System.out.println("You have 5 guesses left.");
        }
        else if(guessnumber == 4){
          System.out.println("You have 4 guesses left.");
        }
        else if(guessnumber == 3){
          System.out.println("You have 3 guesses left.");
        }
        else if(guessnumber == 2){
          System.out.println("You have 2 guesses left.");
        }
        else if(guessnumber == 1){
          System.out.println("You have 1 guess left.");
        }
        else if(guessnumber == 0){
          System.out.println("You have ran out of guesses! Would you like to play again? (y/n)");}

      //TASK 4: if the user has used up the 7 guesses, let the user know that they have run out of guesses
      //they should then have the option of playing again

        if(guessnumber == 0){  
      System.out.println("Play again? (y/n)");
      ans = br.readLine();
        }
      if(ans.equals("n")){
        System.out.println("Thank you for playing!");
         System.out.println("You have gotten" + correctguess + "guessess right");
         System.exit(0);

      }
      else if(ans.equals("y")){
      }
      else{
        System.out.println("Please enter y/n");
      }
     }

    //COMPLETE THE REMAINING TASKS (5-8)
}

  //the numberGenerator method
  //this method generates the random number

public static void numberGenerator(){

    // ********* DO NOT TOUCH ************//
    Random generator = new Random();
    num = generator.nextInt(100) + 1;
    // ***********************************//
}
}

@phorce Here's the code I am trying to run.

Edited 3 Years Ago by bob.henry.1884

This isn't the code I suggested you to use. Anyway, it looks like on line line 27 you terminate the while loop, so therefore, it cannot go any further inside the program. Remove this }

Indent code!!!

P.s You also need to implement the functionality of guessnumber for the program to allow for the number of guesses allowed. I'll give you a hint to this, since I get told off for giving out the answer..

If we Increment a number, we add ++ so i++ and if I was (0) it would increment upwards. 0, 1, 2, ....., 10

In this case, we need to decrement.

I hope this helps.

Edited 3 Years Ago by phorce

//****************************//
//*****DO NOT TOUCH THESE*****//
//****************************//
import java.util.*;
import java.io.*;

class NumberGuessingGame{

  public static int num;    //variable to hold randomly generated number
  public static int guess;  //variable to hold user inputs
  public static String ans; //variable to hold user input for y/n
  public static int guessnumber = 7; //variable to hold number of guesses
  public static int correctguess; //variable to hold number of correct guesses

  //the main method
  public static void main(String[] args) throws IOException{

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

    //introduction
    System.out.println("Welcome to the Number Guessing Game!");

    //loop to allow the user to play again
    ans = "y";
    while(ans.equals("y")){
      numberGenerator();
      System.out.println("\nI have generated a random number between 1 and 100.  Please guess what the number is.");

      //TASK 1: limit the user to only 7 guesses
      guess = Integer.parseInt(br.readLine());

      //TASK 2 & 3: write in the conditions for when the user's guess is too high, too low, or correct
      //if the guess is correct, the user should not be able to keep guessing
      if(guess > num){
        System.out.println("Your guess is too low.");
        guessnumber--;
      }
      else if(guess < num){
        System.out.println("Your guess is too high.");
        guessnumber--;
      }
      else if(guess < 0){
        System.out.println("You have entered a number below 0. Would you like to try again? (y/n)");
      }
      else if(guess < 100){
        System.out.println("You have entered a number above 100. Would you like to try again? (y/n)");
      }
      else if(guess == num){
        System.out.println("CONGRATULATIONS! You have guessed the number correctly! Would you like to play again? (y/n)");
        correctguess++;}

        if(guessnumber == 6){
          System.out.println("You have 6 guesses left.");
        }
        else if(guessnumber == 5){
          System.out.println("You have 5 guesses left.");
        }
        else if(guessnumber == 4){
          System.out.println("You have 4 guesses left.");
        }
        else if(guessnumber == 3){
          System.out.println("You have 3 guesses left.");
        }
        else if(guessnumber == 2){
          System.out.println("You have 2 guesses left.");
        }
        else if(guessnumber == 1){
          System.out.println("You have 1 guess left.");
        }
        else if(guessnumber == 0){
          System.out.println("You have ran out of guesses! Would you like to play again? (y/n)");}

      //TASK 4: if the user has used up the 7 guesses, let the user know that they have run out of guesses
      //they should then have the option of playing again

        if(guessnumber == 0){  
      System.out.println("Play again? (y/n)");
      ans = br.readLine();
        }
      if(ans.equals("n")){
        System.out.println("Thank you for playing!");
         System.out.println("You have gotten" + correctguess + "guessess right");
         System.exit(0);

      }
      else if(ans.equals("y")){
      }
      else{
        System.out.println("Please enter y/n");
      }
     }

    //COMPLETE THE REMAINING TASKS (5-8)


  //the numberGenerator method
  //this method generates the random number

public static void numberGenerator(){

    // ********* DO NOT TOUCH ************//
    Random generator = new Random();
    num = generator.nextInt(100) + 1;
    // ***********************************//
}
  }}

@phorce I have implemented guessnumber and made the changes you have suggested. The problem is now that I get two "Illegal start of expression" errors and two "; required" errors within Line 99.

@bob - Here you go

//****************************//
//*****DO NOT TOUCH THESE*****//
//****************************//
import java.util.*;
import java.io.*;

class NumberGuessingGame{

  public static int num;    //variable to hold randomly generated number
  public static int guess;  //variable to hold user inputs
  public static String ans; //variable to hold user input for y/n
  public static int guessnumber = 7; //variable to hold number of guesses
  public static int correctguess; //variable to hold number of correct guesses

  //the main method
  public static void main(String[] args) throws IOException{

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

    //introduction
    System.out.println("Welcome to the Number Guessing Game!");

    //loop to allow the user to play again
    ans = "y";
    while(ans.equals("y")){
      numberGenerator();
      System.out.println("\nI have generated a random number between 1 and 100.  Please guess what the number is.");

      //TASK 1: limit the user to only 7 guesses
      guess = Integer.parseInt(br.readLine());

      //TASK 2 & 3: write in the conditions for when the user's guess is too high, too low, or correct
      //if the guess is correct, the user should not be able to keep guessing
      if(guess > num){
        System.out.println("Your guess is too low.");
        guessnumber--;
      }
      else if(guess < num){
        System.out.println("Your guess is too high.");
        guessnumber--;
      }
      else if(guess < 0){
        System.out.println("You have entered a number below 0. Would you like to try again? (y/n)");
      }
      else if(guess < 100){
        System.out.println("You have entered a number above 100. Would you like to try again? (y/n)");
      }
      else if(guess == num){
        System.out.println("CONGRATULATIONS! You have guessed the number correctly! Would you like to play again? (y/n)");
        correctguess++;}

        if(guessnumber == 6){
          System.out.println("You have 6 guesses left.");
        }
        else if(guessnumber == 5){
          System.out.println("You have 5 guesses left.");
        }
        else if(guessnumber == 4){
          System.out.println("You have 4 guesses left.");
        }
        else if(guessnumber == 3){
          System.out.println("You have 3 guesses left.");
        }
        else if(guessnumber == 2){
          System.out.println("You have 2 guesses left.");
        }
        else if(guessnumber == 1){
          System.out.println("You have 1 guess left.");
        }
        else if(guessnumber == 0){
          System.out.println("You have ran out of guesses! Would you like to play again? (y/n)");}

      //TASK 4: if the user has used up the 7 guesses, let the user know that they have run out of guesses
      //they should then have the option of playing again

        if(guessnumber == 0){  
      System.out.println("Play again? (y/n)");
      ans = br.readLine();
        }
      if(ans.equals("n")){
        System.out.println("Thank you for playing!");
         System.out.println("You have gotten" + correctguess + "guessess right");
         System.exit(0);

      }
      else if(ans.equals("y")){
      }
      else{
        System.out.println("Please enter y/n");
      }
     }

    //COMPLETE THE REMAINING TASKS (5-8)


  //the numberGenerator method
  //this method generates the random number
}
public static void numberGenerator(){

    // ********* DO NOT TOUCH ************//
    Random generator = new Random();
    num = generator.nextInt(100) + 1;
    // ***********************************//
}}

You were missing the curly brace. Please, indent the code, and format it so such problems would not happen.

Hope this solves the problem. If it does, mark it as solved and give rep to those who helped you

@phorce Thank you! It works now, except with a few bugs. One last question though, how would I change the value of guessnumber back to 7 after it reaches 0?

Edited 3 Years Ago by bob.henry.1884

@bob - Some others may have a better more effective method to do this, but, just inside the while loop you can create an if statement that checks to see if the number is == 0 and if so, put the number back to 7.. Here:

 if(guessnumber == 0)
      {
         guessnumber = 7;
      }

Hope this helps :)

This question has already been answered. Start a new discussion instead.