I need a program that runs an output like this: IN JAVA Hello! Let\'s play the H
ID: 643279 • Letter: I
Question
I need a program that runs an output like this: IN JAVA
Hello! Let's play the Hi-Lo Guessing Game!
First, here are the rules...
I will think of a number from 1 to 10 and then ask you to guess the number. If you guess the number correctly, then I will print a congratulatory message and count the game as a win to you! However, if you guess incorrectly, then I will print either "High" or "Low" according to whether or not your guess was - you guessed it - too high or too low.
You will have three tries to guess the number before it counts as a loss.
At the end of the game, I will ask you if you want to play again.
You can play the game as many times as you like.
Once you indicate that you do not want to play again, I will print the number of games that you have played, the number of times you won the game, and the number of times that you lost the game.
Okay, let's get started!!
I am thinking of a number from 1 to 10...
What do you think it is? 3
Sorry, too low. Please guess again : 7
Sorry, too high. Last try! Guess again: 5
Sorry, the number was 4. That counts as a loss!
Do you want to play again? (yes/no) : yes
Okay, Starting Game 2!
I am thinking of a number from 1 to 10...
What do you think it is? 5
Sorry, too high. Please guess again: 3
You guessed correctly! Congratulations, that counts as a win!
Do you want to play again? (yes/no) : no
You won 1 game and lost 1 game out of 2 games played.
This means you won 50% of your games this time.
Sorry, but this does not make you a superstar. To be a SuperStar you have to win at least 90% of your games!
See you later!
Explanation / Answer
package chegg;
import java.util.Random;
import java.util.Scanner;
public class Guess {
public static void main(String args[])
{
int number,total=0,win=0,lost=0;
boolean flag=false;
Scanner input = new Scanner(System.in);
for(;;)
{
total=total+1;
int x=fun();
flag=false;
System.out.println("I am thinking of a number from 1 to 10...");
for(;;){
System.out.print("What do you think it is?");
number = input.nextInt();
if(x!=number)
{
if(number>x)
System.out.print("Sorry, too high.");
if(number<x)
System.out.print("Sorry, too low. ");
System.out.print("Please guess again :");
number = input.nextInt();
if(x!=number)
{
if(number>x)
System.out.print("Sorry, too high.");
if(number<x)
System.out.print("Sorry, too low. ");
System.out.print("Last try! Guess again:");
number = input.nextInt();
if(x!=number)
System.out.println("Sorry, the number was"+ x +". That counts as a loss!");
else
{
flag=true;
break;
}
}
else
{
flag=true;
break;
}
}
else
{
flag=true;
break;
}
break;
}
if(flag)
{
System.out.println("You guessed correctly! Congratulations, that counts as a win!");
win++;
}
System.out.println("Do you want to play again? (yes/no) ");
String str= input.next();
System.out.println(str);
if(str.equals("no"))
{
lost=total-win;
System.out.println("You won "+win +" game and lost " +lost+" game out of"+ total+" games played.");
float ratio=(float)(win/total);
float percentage=(win*100)/total;
System.out.println("This means you won "+percentage+"% of your games this time.");
if(percentage<90)
System.out.println("Sorry, but this does not make you a superstar. To be a SuperStar you have to win at least 90% of your games!");
else
System.out.println("You are a superstar");
break;
}
if(str.equals("yes"))
continue;
else
{
System.out.println("illegal choice: terminate");
break;
}
}
}
public static int fun()
{
Random randomGenerator = new Random();
return randomGenerator.nextInt(10);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.