任务6、猜数小游戏(单次版)
游戏规则:程序产生一个[1, 100]之间的随机整数,用户输入整数进行猜测,如果猜对了,就结束游戏;如果猜错了,程序会提示你是猜高了还是猜低了,然后让你继续输入整数进行猜测。
public class jask6 {
/*
* 功能:猜数小游戏
* 作者:苏开祥
* 日期:2022年04月07日
*/
public static void main(String[] args) {
// 声明部分
int x, target;
Scanner sc = new Scanner(System.in);
Random random = new Random();
// 产生猜测目标
target = random.nextInt(100);
// 输入猜测的整数
System.out.print("输入一个[1, 100]的整数:");
x = sc.nextInt();
while (x != target) {
// 判断是猜高了还是猜低了
if (x > target) {
System.out.println("朋友,你猜高了~继续猜吧");
} else {
System.out.println("朋友,你猜低了~继续猜吧");
}
// 继续输入猜测的整数
System.out.print("输入一个[1, 100]的整数:");
x = sc.nextInt();
}
System.out.println("恭喜你猜对了,游戏结束~");
}
}