import java.util.Scanner; public class GuessingGame { public static void main(St
ID: 3551065 • Letter: I
Question
import java.util.Scanner;
public class GuessingGame {
public static void main(String[]args){
// creat new Scanner
Scanner input = new Scanner (System.in);
// generationg random number
int randomNumber = 1 + (int)(Math.random()*(100-1+1));
int count=0;
boolean guessing = true;
while (guessing) {
// promte the user to Enter number
System.out.print("Enter number you guess between 1 - 100: ");
int guess = input.nextInt();
count++;
// Check if it is correct , too high or too low
if (guess == randomNumber){
System.out.print("Your guess number is correct.");
guessing = false;
}
if (guess > randomNumber){
System.out.print("Your guess number is too high.");
}
if ( guess < randomNumber){
System.out.print("Your guess number is too low.");
}
System.out.println();
System.out.println("Number of tries " + count);
System.out.println("Thanks for playing");
}
}
}
Explanation / Answer
import java.util.Scanner;
public class GuessingGame{
public static int random(int low,int high)
{
return (1 + (int)(Math.random()*(high-low+1)));
}
public static void main(String[]args){
// creat new Scanner
Scanner input = new Scanner (System.in);
// generationg random number
int randomNumber = random(1,100);
int count=0;
boolean guessing = true;
while (guessing) {
// promte the user to Enter number
System.out.print("Enter number you guess between 1 - 100: ");
int guess = input.nextInt();
count++;
// Check if it is correct , too high or too low
if (guess == randomNumber){
System.out.print("Your guess number is correct.");
guessing = false;
}
if (guess > randomNumber){
System.out.print("Your guess number is too high.");
}
if ( guess < randomNumber){
System.out.print("Your guess number is too low.");
}
System.out.println();
System.out.println("Number of tries " + count);
System.out.println("Thanks for playing");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.