获取随机数组最大值和最小值操作
利用Java的Math类的random()方法,编写函数得到0到n之间的随机数,n是参数。并找出产生50个这样的随机数中最大的、最小的数,并统计其中>=60的有多少个。
提示:使用 int num=(int)(n*Math.random());获取随机数
public class Zuoye { public static void main(String[] args) { Zuoye z = new Zuoye(); z.second(100); } public void second(int n) { int a[] = new int[50]; for (int i = 0; i < 50; i++) { a[i] = (int) (Math.random() * n); } int max = a[0], min = a[0]; int sum = 0; for(int j:a){ if(max<j){ max = j; } if(min>j){ min=j; } if(j>=60){ sum++; } } System.out.println("最大值是:"+max); System.out.println("最小值是:"+min); System.out.println("大于60的数量:"+sum); } }
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。