Question

I have this program that generates a random number that you have to guess. If it's too high or too low, it tells you. I need to to ask after every guess whether or not you want to continue. I've tried using a nested if statement and keep getting all sorts of errors. I'm using jgrasp to code this. I'm new to coding by the way.

import java.util.*;
import java.text.*;

public class redo
{
    public static void main(String [] args)
    {
    //Generate a random number and set values
    Random r = new Random();
    int number = r.nextInt(100);
    System.out.println(number); 
    boolean win = false;

    //Loop    
    while (win == false)
    {

    //Input guess
    System.out.println("Please guess a number 1-100:");
    Scanner scan = new Scanner(System.in);
    int guess = scan.nextInt();

    //Compare input with correct answer
    if  (guess == number)
      { 
         System.out.println("You are correct! the number was " + number + "!");
         win = true;
   } 
      else if (guess > number)
      {
         System.out.println("That number is too high.");
      }
      else if (guess < number)
      {
         System.out.println("That number is too low.");
      } 

  }
  } 
}

EDITED

import java.util.*;
import java.text.*;

public class redo
{
public static void main(String [] args)
{
//Generate a random number and set values
Random r = new Random();
int number = r.nextInt(100);
System.out.println(number); 
   boolean win = false;

   //Loop
   while (win == false)
   {

//Input guess
System.out.println("Please guess a number 1-100:");
    Scanner scan = new Scanner(System.in);
    int guess = scan.nextInt();

//Compare input with correct answer
  if  (guess == number)
  { 
     System.out.println("You are correct! the number was " + number + "!");
     win = true;
   } 
  else if (guess > number)
  {
     System.out.println("That number is too high.");
  }
  else if (guess < number)
  {
     System.out.println("That number is too low.");
  } 

   System.out.println("Would you like to guess again? Please enter Y or N: ");
   String yesorno = scan.next();
   char nooryes = yesorno.charAt(0);
   char No = 'N';
   char no = 'n';
   char Yes = 'Y';
   char yes = 'y';

   while (win == true)

   if (nooryes == No || nooryes == no)
  {
  win = true;
  }
   else if (nooryes == Yes || nooryes == yes)
  {
  win = false;
  }
   else
  {
  System.out.println("I can't read that. Please enter Y or N: ");
  }


      }
   }    
}
Was it helpful?

Solution

You can try adding a new boolean in while loop check.

while (win == false && shouldContinue == true)

And ask the question after,

if  (guess == number)
      { 
         System.out.println("You are correct! the number was " + number + "!");
         win = true;
   } 
      else if (guess > number)
      {
         System.out.println("That number is too high.");
      }
      else if (guess < number)
      {
         System.out.println("That number is too low.");
      } 

By the way, please include your code which gave error, if this does not work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top