打印数组中设定值之间的所有数字

时间:2014-04-13 14:31:06

标签: java arrays random

我想知道如何使用随机数打印数组中设定值之间的所有数字。它还应说明有多少用户随机数高于设定值。以下是它的外观示例:

"How many random numbers between 0 – 999 do you want? 12

These are your numbers

998 168 859 32 533 390 339 243 328 164 536 375

these 8 numbers is between 0 – 499

168 32 390 339 243 328 164 375

these 4 numbers is between 500 – 999

998 859 533 536"

这是我的代码到目前为止,我已经设法让它打印出最大的数字。

public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);                                                    
    System.out.print(" How many random numbers between 0 – 999 do you want? ");     
    int varde = in.nextInt();   
    System.out.println(" ");
    System.out.println(" this is your numbers:");

    int randomArray[]=new int[varde]; 

    for(int i = 0; i<varde; i++)
    {   
        randomArray [i] = (int) (Math.random () * 999); 
        System.out.println(randomArray[i]);
    }

    int largest = randomArray[0]; 
    for(int x=0; x<randomArray.length; x++){ 
        if(randomArray[x]>largest){ 
            largest =  randomArray[x]; 
        } 
    } 

    System.out.print("Largest no is: "+largest); 
}

1 个答案:

答案 0 :(得分:1)

你去。

我所做的是添加java.util.Random类而不是Math.random()来生成随机数。

使用ArrayList,如果需要更多内存,则分配更多内存,以存储低于或大于499的值。

public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);                                                    
    System.out.print(" How many random numbers between 0 – 999 do you want? "); 
    int varde = in.nextInt();   
    System.out.println(" ");
    System.out.println(" this is your numbers:");
    Random randomGenerator = new Random();

    int randomArray[]=new int[varde];

    for(int i = 0; i<varde; i++)
    {   
        randomArray[i] = randomGenerator.nextInt(1000); 
        System.out.print(randomArray[i]);
    }

    int largest = randomArray[0]; 
    for(int x=0; x<randomArray.length; x++){ 
        if(randomArray[x]>largest)
            largest =  randomArray[x]; 
    }
    System.out.println("Largest no is: "+largest); 

    List<Integer> numbersGreaterThanHalf = new ArrayList<Integer>();
    List<Integer> numbersLowerThanHalf = new ArrayList<Integer>();

    for(int x=0; x<randomArray.length; x++){ 
        if(randomArray[x]>499) 
            numbersGreaterThanHalf.add(randomArray[x]);
        else
            numbersLowerThanHalf.add(randomArray[x]);
    }

    System.out.println("these " + numbersLowerThanHalf.length() + "numbers is between 0 – 499");
    for(Integer number: numbersLowerThanHalf)
    {
       System.out.print(number + " ");
    }

    System.out.println("these " + numbersGreaterThanHalf.length() + "numbers is between 500 – 999");
    for(Integer number: numbersGreaterThanHalf)
    {
       System.out.print(number + " ");
    }
}

没有新的数组实现提取:

  StringBuilder numbersAboveHalf = new StringBuilder();
  StringBuilder numberBelowHalf = new StringBuilder();
  int countOfNumbersAboveHalf = 0;
  int countOfNumbersBelowHalf = 0;

  for(int x=0; x<randomArray.length; x++){ 
        if(randomArray[x]>499){
            numbersAboveHalf.append(randomArray[x]).append(" ");
            ++countOfNumbersAboveHalf;
        }
        else{
            numbersBelowHalf.append(randomArray[x]).append(" ");
            ++countOfNumbersBelowHalf;
        }
  }
  System.out.println("these " + countOfNumbersBelowHalf + "numbers is between 0 – 499");
  System.out.println(numbersBelowHalf.toString());
  System.out.println("these " + countOfNumbersAboveHalf + "numbers is between 500 – 999");
  System.out.println(numbersAboveHalf.toString());

这样做是迭代数组并且:

  • 计算500以上的数量以及500以下的数量。
  • 用数字构建2个字符串。