歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> JAVA中Math類的random()方法使用

JAVA中Math類的random()方法使用

日期:2017/3/1 10:44:15   编辑:Linux編程

使用了Math類的random()方法, 由於Math類是出於java.lang包(Package),故使用時不必import這個包。

此外本例還使用了移位運算符

  1. /**
  2. * 使用了Math類的random()方法,
  3. * 由於Math類是出於java.lang包(Package),故使用時不必import這個包。
  4. * <p>
  5. * 此外本例還使用了移位運算符
  6. * @author HAN
  7. *
  8. */
  9. public class Test_random {
  10. public static void main(String[] args) {
  11. char ch=(char)('a'+Math.random()*('z'-'a'+1));
  12. System.out.println(ch);
  13. int a=2;
  14. System.out.println(a<<7);//移位1位相當於乘以2
  15. }
  16. }
Copyright © Linux教程網 All Rights Reserved