import java.io.IOException; import java.util.Random; import java.util.Scanner; p
ID: 3538904 • Letter: I
Question
import java.io.IOException; import java.util.Random; import java.util.Scanner; public class guessinggame2 { public static void main(String[] args) throws IOException { Scanner reader = new Scanner(System.in); Random random = new Random(); int randomNumber, dollars = 0, balance = dollars, guessedNumber = 0; char runAgain = 'y'; randomNumber = random.nextInt(101); System.out.print("Welcome to the Guessing Game!" + " " ); System.out.print("The rules are simple, you have to guess the correct number and " + " "); System.out.print("if it is wrong, you lose $10 and will be prompted on how close " + " "); System.out.print("to the number you are. If you guess the number right, you win " + " "); System.out.print("$100 and can choose whether or not to continue playing or not." + " "); System.out.print("" + " "); System.out.print("How many dollars do you wish to start off with? (Maximum of $300) " + " "); dollars =reader.nextInt(); while (runAgain == 'y' || runAgain == 'Y') { System.out.print("Please pick a number between 1 and 100: "); guessedNumber = reader.nextInt(); while (guessedNumber != randomNumber) { if (guessedNumber < randomNumber) { dollars -= 10; System.out.println("Too low! Guess again! Your balance is: $" + dollars + " "); } if (guessedNumber > randomNumber) { dollars -= 10; System.out.println("Too high Guess again! Your balance is: $" + dollars + " "); } System.out.print("Please pick a number between 1 and 100: "); guessedNumber = reader.nextInt(); } dollars += 50; System.out.println("Congratulations, you got it!" +" "); System.out.println("Your balance is: $" + dollars + " "); System.out.print("Play again (y/n)? "); runAgain = (char)System.in.read(); if (runAgain == 'n' || runAgain == 'N') { System.exit(0); } if (runAgain == 'y' || runAgain == 'Y') randomNumber = random.nextInt(101); } } }Explanation / Answer
I have updated the code as you required, Please see the code below. I have tested, It got worked perfectly. Please test with your inputs.
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class guessinggame2 {
public static void main(String[] args) throws IOException {
Scanner reader = new Scanner(System.in);
Random random = new Random();
int randomNumber, dollars = 0, guessedNumber = 0;
char runAgain = 'y';
System.out.print("Welcome to the Guessing Game!" + " ");
System.out
.print("The rules are simple, you have to guess the correct number and "
+ " ");
System.out
.print("if it is wrong, you lose $10 and will be prompted on how close "
+ " ");
System.out
.print("to the number you are. If you guess the number right, you win "
+ " ");
System.out
.print("$100 and can choose whether or not to continue playing or not."
+ " ");
System.out.print("" + " ");
System.out
.print("How many dollars do you wish to start off with? (Maximum of $300) "
+ " ");
dollars = reader.nextInt();
do{
//This has to be inside, why because every time random nummber need to be changed.
//If you put outside. It will be same for all iterations.
randomNumber = random.nextInt(101);
System.out.print("Please pick a number between 1 and 100: ");
guessedNumber = reader.nextInt();
while (guessedNumber != randomNumber) {
//check if the amount is there or not
//if not there it will exit.
//Just updated, If you don't like it, Please remove it.
if(dollars <= 0){
System.out.println("No balance in the account.... Exit.........");
System.exit(0);
}
dollars -= 10;
if (guessedNumber < randomNumber) {
System.out
.println("Too low! Guess again! Your balance is: $"
+ dollars + " ");
}else if (guessedNumber > randomNumber) {
System.out
.println("Too high Guess again! Your balance is: $"
+ dollars + " ");
}
System.out.print("Please pick a number between 1 and 100: ");
guessedNumber = reader.nextInt();
}
dollars += 100;
System.out.println("Congratulations, you got it!" + " ");
System.out.println("Your balance is: $" + dollars + " ");
while(true){
System.out.print("Play again (y/n)? ");
runAgain = (char) System.in.read();
System.in.skip(2);
if(runAgain == 'n' || runAgain == 'N' || runAgain == 'y' || runAgain == 'Y'){
break;
}else{
System.out.println("Invalid input... Please try again");
}
}
if (runAgain == 'n' || runAgain == 'N') {
System.exit(0);
}
}while(true);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.