生成100個0-99的隨機整數,找出最大和最小值,並統計大於50的整數個數



生成100個0-99的隨機整數,找出最大和最小值,並統計大於50的整數個數
提示:
Math類支持random方法:
public static synchronized double random()
該方法返回一個0.0-1.0的小數,如果要得到其他范圍的數,需要進行相應的轉換。
例如:0-99的整數 int num=(int)(100*Math.random());

public class Radom {
public static void main(String args[]){
int[] a = new int[100];
int count = 0;
int Max = 0,Min=100;
for(int i=0;i<100;i++){
a[i] = (int)(100*Math.random());
if(a[i]>50){
count++;
}
if(a[i]>Max){
Max = a[i];
}
if(a[i]<Min){
Min = a[i];
}
}
System.out.println("0-99隨機整數中最大值為:"+Max);
System.out.println("0-99隨機整數中最小值為:"+Min);
System.out.println("0-99隨機整數中大於50的整數個數為::"+count);
for(int j=0;j<100;j++){
System.out.print(a[j]+" ");
}
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM