日期:2014-05-20  浏览次数:20597 次

java编程思想的一个程序
先来代码
Java code

public class RandomBounds {
    static void usage() {
        System.out.println("Usage: \n\t" + "RandomBounds lower\n\tRandomBounds upper");
        System.exit(1);
    }

    public static void main(String[] args) {
        if(args.length != 1)
            usage();
        if(args[0].equals("lower")) {
            while(Math.random() != 0.0)
                ;    //Keep trying
            System.out.println("Produced 0.0!");
        }
        else if(args[0].equals("upper")) {
            while(Math.random() != 1.0)
                ;  //keep trying
            System.out.println("Produced 1.0!");
        }
    }
}




if(args.length != 1) --------------这args是从哪来的呀!?(String[] args) 什么意思!
另外说这个程序是展示Random()函数的范围[0,1) , 怎么没看懂!

显示结果

usage:
  RandomBounds lower
  RandomBounds upper


------解决方案--------------------
String[] args是main方法的参数,在命令行输入java xxx时执行时,会将命令行各字符串作为参数存储在数组args中,在main中则可以通过这个数组取得输入的各参数。
------解决方案--------------------
这个args就是指的输入的参数 java class类名 参数1 参数2 参数3 ... 也可以没有参数。
另外说这个程序是展示Random()函数的范围[0,1);
if(args[0].equals("lower")) {
while(Math.random() != 0.0)
; //Keep trying
System.out.println("Produced 0.0!");
}
如果参数1是lower。 while(Math.random() != 0.0);循环直到产生0 停止循环打印 Produced 0.0!
如果参数1是upper。 while(Math.random() != 0.0);循环直到产生1 停止循环打印 Produced 1.0!但是你会发现是个循环,打印不出来。
------解决方案--------------------
楼上两位正解
------解决方案--------------------
Math.random()本来产生的随即数就时0~1之间的啊!你只不过用0.0和1.0控制循环退出而已。。。貌似时死循环,印象中Math.random()产的值达不到极端。。