小编典典

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

java

我想知道如何在具有随机数的数组中的设置值之间打印所有数字。它还应说明有多少用户随机数高于设置值。这是一个外观的示例:

"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); 
}

阅读 212

收藏
2020-11-30

共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个字符串。
2020-11-30