So the program is to create a guessing game. Which i have done. What i am having
ID: 3634046 • Letter: S
Question
So the program is to create a guessing game. Which i have done. What i am having difficulties is adding a sentinal value "0" so that the user can exit the game.Also i need to add a option where it says "would you like to play again" (y/n)
My current code
package q2;
import java.util.*;
/**
* <p>This is where you put your description about what this class does. You
* don't have to write an essay but you should describe exactly what it does.
* Describing it will help you to understand the programming problem better.</p>
*
* @author Your Name goes here
* @version 1.0
*/
public class Guess {
/**
* <p>This is the main method (entry point) that gets called by the JVM.</p>
*
* @param args command line arguments.
*/
public static void main(String[] args) {
// your code will go here!!!
String another = "y";
int numToGuess; //Number the user tries to guess
int guess; //The user's guess
int highGuess = 0;
int lowGuess = 0;
int numOfGuesses=0;
Scanner scan = new Scanner(System.in);
Random generator = new Random();
//randomly generate the number to guess
numToGuess = generator.nextInt(100)+1;
//print message asking user to enter a guess
System.out.println("Lets play a guessing game, choose a number between (1 - 100) type 0 to exit the game ");
//read in guess
guess = scan.nextInt();
while (another.equalsIgnoreCase("y"))
{
while (guess != numToGuess ) //keep going as long as the guess is wrong
{
//print message saying guess is wrong
if (guess < numToGuess )
{
System.out.println("TRY GUESSING HIGHER!");
lowGuess++;
}
else
{
System.out.println("Try guessing lower ");
highGuess++;
}
//get another guess from the user
System.out.println("Enter your next guess?");
guess = scan.nextInt();
numOfGuesses++;
}
//print message saying guess is right
System.out.println("Winner winner chicken dinner! ");
System.out.println("Total of Low Guesses: " + lowGuess);
System.out.println("Total of High Guesses: " + highGuess);
System.out.println("Total of all your guesses: " + numOfGuesses);
System.out.println();
System.out.println("play again? (y/n)? ");
another =scan.nextLine();
System.out.println("Question 2 was called successfully");
}
}
};
Thanks if anyone can help
Explanation / Answer
please rate - thanks
I think I highlighted all the changes I made
package q2;
import java.util.*;
/**
* <p>This is where you put your description about what this class does. You
* don't have to write an essay but you should describe exactly what it does.
* Describing it will help you to understand the programming problem better.</p>
*
* @author Your Name goes here
* @version 1.0
*/
public class Guess {
/**
* <p>This is the main method (entry point) that gets called by the JVM.</p>
*
* @param args command line arguments.
*/
public static void main(String[] args) {
// your code will go here!!!
String another = "y";
int numToGuess; //Number the user tries to guess
int guess; //The user's guess
int highGuess = 0;
int lowGuess = 0;
int numOfGuesses=0;
Scanner scan = new Scanner(System.in);
Random generator = new Random();
while (another.equalsIgnoreCase("y"))
{
//randomly generate the number to guess
numToGuess = generator.nextInt(100)+1;
//print message asking user to enter a guess
System.out.println("Lets play a guessing game, choose a number between (1 - 100) type 0 to exit the game ");
//read in guess
guess = scan.nextInt();
while (guess != numToGuess&&guess!=0 ) //keep going as long as the guess is wrong
{
//print message saying guess is wrong
if (guess < numToGuess )
{
System.out.println("TRY GUESSING HIGHER!");
lowGuess++;
}
else
{
System.out.println("Try guessing lower ");
highGuess++;
}
//get another guess from the user
System.out.println("Enter your next guess?");
guess = scan.nextInt();
numOfGuesses++;
}
if(guess==0)
System.out.println("You quit :( :(");
else
//print message saying guess is right
System.out.println("Winner winner chicken dinner! ");
System.out.println("Total of Low Guesses: " + lowGuess);
System.out.println("Total of High Guesses: " + highGuess);
System.out.println("Total of all your guesses: " + numOfGuesses);
System.out.println();
System.out.println("play again? (y/n)? ");
another =scan.next();
System.out.println("Question 2 was called successfully");
}
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.