Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The system will generate a random number between 0 and 100 int number = ( int )(

ID: 3731169 • Letter: T

Question

The system will generate a random number between 0 and 100

int number = (int)(Math.random() * 101);

Then start asking the user to guess the number.

If the user guesses the number – tell them success

If the user guesses a number lower than number tell them their number is too low

If the user guesses a number higher than the number tell them they are too high

Repeat asking the user until they get the correct number.

Keep track of the number of guesses - If more than 8 guesses tell the user they got a C. If between 4 and 7 guesses the user got a B. If less than 4 guesses the user gets a A. Use a switch statement to determine what letter grade they got.

Explanation / Answer

import java.util.Random;
import java.util.Scanner;

class Main {
public static void main(String[] args) {
  
// GENERATING a random number
int number = (int)(Math.random() * 101);
Scanner sc = new Scanner(System.in);
int guess, count = 0;
  
System.out.print(" Enter number: ");
while(true)
{
// taking user input for guess
guess = sc.nextInt();
count++;
  
// low
if(guess < number)
{
System.out.println("Your number is too low.");
System.out.print(" Enter number: ");
}
  
// high
else if(guess > number)
{
System.out.println("Your number is too high.");
System.out.print(" Enter number: ");
}
  
// equal, breaking loop
else
{
break;
}
}
switch(count)
{
case 1:
case 2:
case 3:
System.out.println("You got a A");
break;
case 4:
case 5:
case 6:
case 7:
case 8:
System.out.println("You got a B");
break;
default:
System.out.println("You got a C");
}
}
}

/*SAMPLE OUTPUT 1

Enter number: 50
Your number is too high.

Enter number: 25
Your number is too high.

Enter number: 12
Your number is too high.

Enter number: 9
Your number is too high.

Enter number: 4
Your number is too high.

Enter number: 3
Your number is too high.

Enter number: 2
You got a B

--- Sample output 2
Enter number: 23
Your number is too low.

Enter number: 45
Your number is too high.

Enter number: 37
You got a A
*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote