1. 首页
  2. 编程语言
  3. Actionscript
  4. Java社区文艺汇演小游戏代码

Java社区文艺汇演小游戏代码

上传者: 2024-04-20 23:19:14上传 DOCX文件 14.67KB 热度 9次

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);

}

}

用户评论