import java.util.Random;
public class RandomNumber {
public void maxMin(int[] nums)
{
nums = new int[10];
int max = 0;
int least = 0;
Random rand = new Random();
for(int i=0;i<nums.length;i++)
{
nums[i]=rand.nextInt(1000)+1;
}
//最大值
max = nums[0];
for(int i=0;i<nums.length;i++)
{
if(nums[i]>max)
{
max = nums[i];
}
}
//最小值
least = nums[0];
for(int i=1;i<nums.length;i++)
{
if(nums[i]<least)
{
least = nums[i];
}
}
System.out.println("随机数为:");
for(int j=0;j<10;j++){
System.out.print(nums[j]);
System.out.print(",");
}
System.out.println();
System.out.println("最大的数是:" +max);
System.out.println("最小的数是: "+least);
}
public static void main(String[] args){
RandomNumber randomNumber = new RandomNumber();
int[] nums = new int[10];
Random rand = new Random();
for(int i=0;i<nums.length;i++)
{
nums[i]=rand.nextInt(1000)+1;
}
randomNumber.maxMin(nums);
}
}
追问请问随机产生的10个数怎么保存到一个数组中去?
追答看代码中:nums[i]=rand.nextInt(1000)+1;
这就是啊,