This is the code I have done so far, but I can\'t get it to work. Can someone co
ID: 3629417 • Letter: T
Question
This is the code I have done so far, but I can't get it to work. Can someone correct this code so that it compiles and executes without errors?These are the instructions******************
/*Modify the program of Exercise 6.30 to count the number of guesses the player makes.
If the number is 10 or fewer, display Either you know the secret or you got lucky! If the player guesses
the number in 10 tries, display Aha! You know the secret! If the player makes more than 10 guesses, display
You should be able to do better! Why should it take no more than 10 guesses? Well, with each "good guess," the player should be able to elimiate
half of the numbers, then half of the remaining numbers, and so on.
*/
import java.util.Scanner;
public class GuessNumber
{
int count;
public GuessNumber()
{
count = 0;
}
public void game()
{
int answer = getNumber();
int guessNumber;
do
{
guessNumber = GuessGame();
if (guessnumber < answer )
{
System.out.println(guessnumber + " is too low. Try again.");
}
else displayMessage();
}while (guessnumber != answer );
}
public int GuessGame()
{
Scanner input = new Scanner(System.in);
System.out.println( "Guess a number between 1 and 1000: ");
int number = input.nextInt();
count++;
return number;
}
public int getNumber()
{
return( ( int ) ) (1 = Math.random() * 1000 ) );
}
//Print a message based on the number of tries
public void displayMessage()
{
if ( count < 10)
System.out.println( "You guessed the number!"
+ " Either you know the secret or you got lucky!");
else if ( count == 10 )
System.out.println( "You guessed the number! Aha! You " +
"know the secret!");
else System.out.println( "You guessed the number! You should" +
"be able to do better!");
public class Guess2
{
public static void main( String[] args )
{
GuessNumber num = new GuessNumber();
num.game();
}
}
Explanation / Answer
i fix some stuff in the code and i compiled it and it runs perfectly this is the code. import java.util.Scanner; public class GuessNumber { int count; public GuessNumber() { count = 0; } public void game() { int answer = getNumber(); int guessNumber; do { guessNumber = GuessGame(); if (guessNumberRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.