码迷,mamicode.com
首页 > 编程语言 > 详细

JavaSE 高级 第03节 Math类与猜数字游戏

时间:2016-07-24 17:46:00      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

2016-07-24

1,Math类介绍

         Math.Random()  [0,1)

2,猜数字游戏

         课下作业:猜数字小程序优化,增加次数限制的功能。

package com.java1995;

public class TestMath {

    public static void main(String[] args) {
        double d = Math.ceil(2.98);
        System.out.println(d);

        d = Math.floor(2.98);
        System.out.println(d);

        System.out.println(Math.max(6.5, 4.6));

        for (int i = 0; i < 10; i++) {
            System.out.println(Math.random());
        }
    }
}

 

package com.java1995;

import java.util.Scanner;

public class GuessNumber {

    public static void main(String[] args) {
        int num = (int) (Math.random() * 100) + 1;// [1,100]
        System.out.println("请输入1-100之间的整数");
        Scanner sc = new Scanner(System.in);
        while (true) {
            int input = sc.nextInt();
            if (input > num) {
                System.out.println("太大了");
            } else if (input < num) {
                System.out.println("太小了");
            } else {
                System.out.println("恭喜你,猜对了!");
                break;
            }
        }
        // sc.close();
    }
}

技术分享

 

【参考资料】

[1] Java轻松入门经典教程【完整版】

JavaSE 高级 第03节 Math类与猜数字游戏

标签:

原文地址:http://www.cnblogs.com/cenliang/p/5701121.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!