If anyone can help me solve this problem, i would appreciate it. I have to use t
ID: 669595 • Letter: I
Question
If anyone can help me solve this problem, i would appreciate it. I have to use three "if" statements, one while loop, and three "for" loops to display the following:
Guess the number I'm thinking of, from 1-32: 16
Your guess is larger than the random value. Next guess:
8
Your guess is larger than the random value. Next guess:
4
Your guess is smaller than the random value. Next guess:
6
You've guessed correct!
Total number of guesses: 4
****
Smaller Guesses: 1
*
Larger Guesses: 2
**
//This is what i have so far from my code, and i guess im just stuck on the asterisk part :
import java.util.Scanner;
import java.util.Random;
public class testing {
public static void main(String [] args){
Random number = new Random();
int numbertoguess = number.nextInt(32)+1;
Scanner input = new Scanner(System.in);
boolean win = false;
int numberoftries = 0;
int guess;
while(win == false){
System.out.println("Guess a number: ");
guess = input.nextInt();
numberoftries++;
if(guess == numbertoguess){
System.out.println(" Guess of the number I'm thinking of, from 1-32 : " );
win = true;
}
if(guess > 32 || guess <0){
System.out.println("Please try again: ");
}
else if (guess < numbertoguess){
System.out.println(" Your guess is smaller than the random value. Next guess: ");
}
else if(guess > numbertoguess ){
System.out.println(" Your guess is larger than the random value. Next guess:" );
}
}
System.out.println(" Game over" + " You've guessed correct! ");
System.out.printf(" Total number of guesses: " + numberoftries);
System.out.println(" The answer was: " + numbertoguess );
}
}
Explanation / Answer
//This is what i have so far from my code, and i guess im just stuck on the asterisk part :
import java.util.Scanner;
import java.util.Random;
public class testing {
public static void main(String [] args){
Random number = new Random();
int numbertoguess = number.nextInt(32)+1;
Scanner input = new Scanner(System.in);
boolean win = false;
int smallerguesses = 0;
int largerguesses = 0;
int numberoftries = 0;
int guess;
while(win == false){
System.out.println("Guess a number: ");
guess = input.nextInt();
numberoftries++;
if(guess == numbertoguess){
System.out.println(" Guess of the number I'm thinking of, from 1-32 : " );
win = true;
}
if(guess > 32 || guess <0){
System.out.println("Please try again: ");
}
else if (guess < numbertoguess){
smallerguesses++;
System.out.println(" Your guess is smaller than the random value. Next guess: ");
}
else if(guess > numbertoguess ){
largerguesses++;
System.out.println(" Your guess is larger than the random value. Next guess:" );
}
}
System.out.println(" Game over" + " You've guessed correct! ");
System.out.printf(" Total number of guesses: " + numberoftries);
System.out.println(" The answer was: " + numbertoguess );
for(int i = 0 ; i < numberoftries; i++)
System.out.print("*");
System.out.println("");
for(int i = 0 l i < smallerguesses; i++) System.out.print("*");
System.out.println("");
for(int i = 0 ; i < largerguesses; i++ ) System.out.println("*");
}
}