猜数小游戏(单次版)
· 游戏规则:程序产生一个[1, 100]之间的随机整数,用户输入整数进行猜测,如果猜对了,就结束游戏;如果猜错了,程序会提示你是猜高了还是猜低了,然后让你继续输入整数进行猜测。
(一)编程实现
创建Task_06
/**猜数小游戏
-
功能:
-
作者:李悦
-
日期:2022年04月14日
*/
public class Task_06 {
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 { System.out.println("朋友,你猜低了,继续猜吧~"); } //用户输入猜测的数 System.out.print("输入你猜的整数:"); x = sc.nextInt(); } System.out.print("恭喜你,猜对了,游戏结束!");
}
}
· 运行结果
· 创建Task_06_01
运行结果
(二)拓展练习
任务1、猜数小游戏(多次版)
· 如果用户猜对了,会询问用户是否继续玩。
任务2、构建可进可退的多级菜单系统
创建XExercise02类
· 运行结果