文章目录
猜数小游戏
- 游戏规则:程序产生一个[1, 100]之间的随机整数,用户输入整数进行猜测,如果猜对了,就结束游戏;如果猜错了,程序会提示你是猜高了还是猜低了,然后让你继续输入整数进行猜测。
(一)编程实现
-
public class tesk01 {
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("恭喜你,猜对了,游戏结束!");
}
} -
运行结果
(二)拓展练习
1、猜数小游戏
- 如果用户猜对了,会询问用户是否继续玩。