int[] arr = new int[5];
int max,sum,temp;
Random r = new Random();
for(int i =0;i
arr[i] = temp;
max = max>temp?max:temp;
sum+=temp;
}
System.out.println("max:"+max);
System.out.println("sum:"+sum)
这种完全可以直接理解成,生成5个随机数,求这5个数的最大值和总和,根数组没有半毛钱关系啊,完全可以不用数组就能实现了
import java.util.Random;
public class Demo {
public static void main(String[] args) {
Random random = new Random();
int sum,max;
sum=max= random.nextInt();
System.out.println(max);
for(int i=0;i<4;i++)
{
int ram = random.nextInt();
System.out.println(ram);
sum+=ram;
max = ram > max?ram:max;
}
System.out.println("max:"+max);
System.out.println("sum:"+sum);
}
}
int a[] = new int[5];
int max=100;
int min=1;
Random random = new Random();
int s = 0;
int m = 0;
for (int i = 0; i < a.length; i++) {
a[i]=random.nextInt(max)%(max-min+1) + min;
if(i>0){
m = a[i]>m?m:a[i];
}else{
m = a[i];
}
s += a[i];
}
System.out.println("最大值为:"+m+",和为:"+s);
好像数组是没啥用_(:з」∠)_