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


使用了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.           
  14.         int a=2;  
  15.         System.out.println(a<<7);//移位1位相当于乘以2   
  16.     }  
  17.   
  18.   
  19. }  

相关内容