/**
*功能:猜数小游戏(随机数)
*作者:许锐
*创作时间:2022年3月14日
*/
public class Task02 {
public static void main(String[] args) {
//声明部分
int target, x;
Scanner sc = new Scanner(System.in);
//产生猜测目标
Random random = new Random();
target = random.nextInt(100);
//用户输入猜测的数
System.out.print("请输入你猜测的整数:");
x = sc.nextInt();
//判断用户是否猜测正确
while (x != target){
if (x> target) {
System.out.println("朋友,你猜多了,继续猜哦~~");
}else if (x < target) {
System.out.println("朋友,你猜少了,继续猜哦~~");
} else {
break; //猜中了,跳出循环
}
//用户输入猜测的数
System.out.print("请输入你猜测的整数:");
x = sc.nextInt();
}
System.out.println("恭喜,你猜对了,游戏结束!");
}
}
猜数小游戏
于 2022-04-14 08:45:02 首次发布