Java社区文艺汇演小游戏代码
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random random = new Random();
int secretNumber = random.nextInt(100) + 1;
int guessCount = 0;
Scanner scanner = new Scanner(System.in);
while (guessCount < 10) {
System.out.print("请输入你猜的数字:");
int guess = scanner.nextInt();
guessCount++;
if (guess == secretNumber) {
System.out.println("恭喜你猜对了!");
return;
} else if (guess < secretNumber) {
System.out.println("猜的数字太小了!");
} else {
System.out.println("猜的数字太大了!");
}
}
System.out.println("很遗憾,你没有在规定次数内猜对,正确答案是" + secretNumber);
}
}
用户评论