Files
workspace/java/guessnum.java
e2hang ebcee63b7c New
2026-01-09 00:05:37 +08:00

29 lines
914 B
Java

import java.util.Random;
import java.util.Scanner;
public class guessnum{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int cnt = 0;
int guess = -1;
int target = random.nextInt(1024) + 1;
while(guess != target){
System.out.println("Input Your Guess:");
if(scanner.hasNextInt()){
int tmp = scanner.nextInt();
cnt++;
if(guess < target){
System.out.println("It's Smaller than the word");
} else if(guess > target){
System.out.println("It's Bigger than the word");
}
else {
System.out.println("You've Guessed Right with " + cnt + " Attempts");
}
}
}
scanner.close();
}
}