项目介绍:
这是一个基于Java语言编写的控制台应用程序,它实现了一个猜数字游戏。程序会随机生成一个0~100之间的整数,玩家需要通过不断猜测数字来找出答案。程序会提示每次猜测的结果,例如“你猜的数字太大了”、“你猜的数字太小了”、“恭喜你,猜对了!”等等。
项目实现:
以下是项目的基本实现步骤:
- 生成一个0~100之间的随机整数,作为答案。
- 提示玩家输入猜测的数字。
- 将玩家的输入与答案进行比较。
- 如果玩家猜对了,输出恭喜信息,并退出程序。
- 如果玩家猜错了,根据大小关系输出提示信息,然后返回步骤2。
public class GuseeNumber { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Random random = new Random(); int i = random.nextInt(101);// 生成0~100的随机整数,作为答案 int count =5; while (count>0){ System.out.println("请输入你猜想的数字"); int guess = scanner.nextInt(); if(i ==guess){ System.out.println("挑战成功,"+"答案为"+i); break; } if (i<guess){ System.out.println("大了大了"); } if (i>guess){ System.out.print("小了小了"+"\t"); } count--; if(count>1){ System.out.println("你还有"+count+"次机会"); } } if(count==0){ System.out.println("挑战失败,"+"答案为"+i); } } }