猜数字小游戏
import java.util.Random;
import java.util.Scanner;
public class GuessNumber {
public static void main(String[] args) {
int number = (int) (Math.random() * 100) + 1;
Scanner sc = new Scanner(System.in);
/* Random r = new Random();
int number = r.nextInt(100)+1;*/
// Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入一个1~100的数字:");
int guess = sc.nextInt();
if (guess > 0 && guess <= 100) {
if (guess > number) {
System.out.println("你输入的数字" + guess + "大了!");
} else if (guess < number) {
System.out.println("你输入的数字" + guess + "小了!");
} else {
System.out.println("恭喜你!猜中了!");
break;
}
} else {
System.out.println("你输入的数字有误!");
}
}
}
} |
|