A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© shuide 中级黑马   /  2015-10-11 22:03  /  707 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/*猜数字小游戏--测智商小游戏
* 需求:猜数字小游戏(数据在1-100之间)
利用Math.random()
思路:
猜选次数用计数器思想 count
随机一个数字Math.random()
*/
import java.util.Scanner;                                                        //引入包
class Test_Math {
        public static void main(String[] args) {

                Scanner sc = new Scanner(System.in);
                System.out.println("请输入一个数字1-100之间");
       
        int count = 0;                                                                        //定义count计数器
        int aa = (int)(Math.random() * 100 + 1);                //随机一个 数
       
        while (true) {
                int temp = sc.nextInt();
                if (temp < aa) {
                        count++;
                        System.out.println("小了");
                }
                else if (temp > aa) {
                        count++;
                        System.out.println("大了");
                }else {
                        count++;
                        System.out.println("对了");
                        if (count < 3) {
                                System.out.println("智商聪明" + " 猜的次数" + count);
                        }else if (count < 10) {
                                System.out.println("智商正常" + " 猜的次数" + count);
                        }else {
                                System.out.println("智商不聪明" + " 猜的次数" + count);
                        }
                        break;
                }
        }
        }

}

3 个回复

倒序浏览
试了一下 有意思
回复 使用道具 举报
count++;直接放在 int temp = sc.nextInt();下面更好点
回复 使用道具 举报
michael_wlq 发表于 2015-10-12 13:32
count++;直接放在 int temp = sc.nextInt();下面更好点

说得对.......
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马