write a java program (beginner) output and comment is sooo imp- guess a number •
ID: 3595244 • Letter: W
Question
write a java program (beginner) output and comment is sooo imp-
guess a number
• 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
Keep asking the user until they get it right.
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
Hi Let me know if you need more information:-
================================
import java.util.Scanner;
/**
*
*/
/**
* @author XXXXXX
*
*/
public class GuessNumber {
/**
*
*/
public GuessNumber() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
@SuppressWarnings("resource")
public static void main(String[] args) {
int count = 0;
Scanner reader = new Scanner(System.in);
while (true) {
int number = (int) (Math.random() * 101);
System.out.println("Guess the number.");
//System.out.println(number); Testing purpose
int input = reader.nextInt();
count++;
if (input < number) {
System.out.println("number is too low");
} else if (input > number) {
System.out.println("number is too high");
} else if (input == number) {
System.out.println("success");
break;
} else {
// must not happenn
}
}
if(count >8) {
System.out.println("C Grade");
}else if(count >4 || count <7) {
System.out.println("B Grade");
}else if(count <4) {
System.out.println("A Grade");
}
}
}
=================
Guess the number.
7
number is too low
Guess the number.
2
success
B Grade
=================
Thanks
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.