JAVA数字处理类使用


开发了一个程序,功能如下: 获取2~32之间的6个整数,并取得这6个偶数的和。

本例中引用了Math.random(),但是实际上实现的是Random.nextDouble()。 只是对于一般的使用random的情况,较Random类,我们习惯使用Math.random() mainly because it si simpler to use

  1. /** 
  2.  * 开发了一个程序,功能如下: 
  3.  * 获取2~32之间的6个整数,并取得这6个偶数的和。 
  4.  * <p> 
  5.  * 本例中引用了Math.random(),但是实际上实现的是Random.nextDouble()。 
  6.  * 只是对于一般的使用random的情况,较Random类,我们习惯使用Math.random() mainly because it is simpler to use 
  7.  * @author HAN 
  8.  * 
  9.  */  
  10. public class DataTreatementClassApps {  
  11.       
  12.      
  13.     public static void main(String[] args) {  
  14.          int i=0;  
  15.          int sum=0;  
  16.             while(i<6){  
  17.                 int a=(int) (2+Math.random()*30);   
  18.                   
  19.                 if (a%2==0){  //取余方法   
  20.                     System.out.println(a);  
  21.                     sum=sum+a;  
  22.                     i++;  
  23.                 }  
  24.                   
  25.             }  
  26.             System.out.println("六个偶数的和:"+sum);  
  27.               
  28.   
  29.     }  
  30.   
  31. }  
  • 1
  • 2
  • 下一页

相关内容