HiLoGame.java Guessing Game program using if else and while loop
//*****************************************************************************
// HiLoGame.java
// Guessing game program using if else and while loop.
//******************************************************************************
import java.util.Scanner;
public class HiLoGame {
public static void main(String[] args) {
System.out.println("Welcome to Number Guessing Game, Guess number between 1 to 100");
Scanner scanner = new Scanner(System.in);
int number= (1 + (int)(Math.random()*100));
int guess = -1;
while (guess!=number) {
System.out.print("Enter your guess: ");
guess = scanner.nextInt();
if (guess<number) {
System.out.println("Number is too low, please try again");
} else if (guess>number) {
System.out.println("Number is too high, please try again");
} else {
System.out.println("Yeppi, good guessing, the number is correct, the number is " + number);
}
}}}
No comments:
Post a Comment