话不多说直接上代码:
public class Guess {
public static void main(String[] args) {
Random rand = new Random();
int num = rand.nextInt(100) + 1;
Scanner sc = new Scanner(System.in);
int index = 0;
while (true) {
++index;
System.out.print("请输入数字:");
int n = sc.nextInt();
if (n > num) {
System.out.printf("%d、太大了%n", index);
} else if (n < num) {
System.out.printf("%d、太小了%n", index);
} else {
if (index == 1) {
System.out.printf("大神,恭喜您,猜对了,你的游戏成绩(100 分)%n");
} else if(index>=10) {
System.out.printf("恭喜您,猜对了,但是你的游戏成绩(0 分)%n");
}
else {
System.out.printf("%d、恭喜您,猜对了,你的游戏成绩(%d 分)%n", index, 100 - index * 10);
}
break;
}
}
}
}
|