本文介绍了打印所有号码设定值之间的一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何打印插图中设定值的所有号码在随机数的数组。还应该说明有多少用户随机数的设定值以上。这里是它如何看一个例子:

 有多少随机数的0  -  999你要12?这些都是你的号码998 168 859 32 533 390 339 243 328 164 536 375这8个数字是0之间 -  499168 32 390 339 243 328 164 375这4个数字是500之间 -  999998 859 533 536

这是我的code到目前为止,我设法得到它的打印数量最多。

 公共静态无效的主要(字串[] args)
{
    扫描仪在=新的扫描仪(System.in);
    System.out.print(有多少随机数的0 - 999你要干什么?);
    INT瓦德= in.nextInt();
    的System.out.println();
    的System.out.println(这是你的数字:);    INT randomArray [] =新的INT [瓦德]    的for(int i = 0; I<瓦德,我++)
    {
        randomArray [I] =(INT)(的Math.random()* 999);
        的System.out.println(randomArray [I]);
    }    诠释最大= randomArray [0];
    为(中间体X = 0; X&下; randomArray.length; X ++){
        如果(randomArray [X]≥最大){
            最大= randomArray [X];
        }
    }    System.out.print(最大无是:+最大);
}


解决方案

你去那里。

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

用于一个的ArrayList 如果需要更多的,用于存储比499更低或更高值的分配更多的内存。

 公共静态无效的主要(字串[] args)
{
    扫描仪在=新的扫描仪(System.in);
    System.out.print(有多少随机数的0 - 999你要干什么?);
    INT瓦德= in.nextInt();
    的System.out.println();
    的System.out.println(这是你的数字:);
    随机randomGenerator =新的随机();    INT randomArray [] =新的INT [瓦德]    的for(int i = 0; I<瓦德,我++)
    {
        randomArray [I] = randomGenerator.nextInt(1000);
        System.out.print(randomArray [I]);
    }    诠释最大= randomArray [0];
    为(中间体X = 0; X&下; randomArray.length; X ++){
        如果(randomArray [X]≥最大)
            最大= randomArray [X];
    }
    的System.out.println(最大无是:+最大);    清单<整数GT; numbersGreaterThanHalf =新的ArrayList<整数GT;();
    清单<整数GT; numbersLowerThanHalf =新的ArrayList<整数GT;();    为(中间体X = 0; X&下; randomArray.length; X ++){
        如果(randomArray [X]> 499)
            numbersGreaterThanHalf.add(randomArray [X]);
        其他
            numbersLowerThanHalf.add(randomArray [X]);
    }    的System.out.println(本+ numbersLowerThanHalf.length()+号在0 - 499);
    对于(整数数:numbersLowerThanHalf)
    {
       System.out.print(编号+);
    }    的System.out.println(本+ numbersGreaterThanHalf.length()+号是500之间 - 999);
    对于(整数数:numbersGreaterThanHalf)
    {
       System.out.print(编号+);
    }
}

没有新阵列实现萃取:

 的StringBuilder numbersAboveHalf =新的StringBuilder();
  StringBuilder的numberBelowHalf =新的StringBuilder();
  INT countOfNumbersAboveHalf = 0;
  INT countOfNumbersBelowHalf = 0;  为(中间体X = 0; X&下; randomArray.length; X ++){
        如果(randomArray [X]> 499){
            。numbersAboveHalf.append(randomArray [X])附加();
            ++ countOfNumbersAboveHalf;
        }
        其他{
            。numbersBelowHalf.append(randomArray [X])附加();
            ++ countOfNumbersBelowHalf;
        }
  }
  的System.out.println(本+ countOfNumbersBelowHalf +号在0 - 499);
  的System.out.println(numbersBelowHalf.toString());
  的System.out.println(本+ countOfNumbersAboveHalf +号是500之间 - 999);
  的System.out.println(numbersAboveHalf.toString());

这样做是迭代这个数组和:


  • 算多少数量超过500在那里,有多少是低于500。

  • 2建立与字符串的数字。

Im wondering how to print all numbers inbetween a set value in an array with random numbers. It should also state how many of the users random numbers are above the set value. Here is an example of how it could look:

"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"

this is my code so far, i have managed to get it to print the largest number.

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); 
}
解决方案

There you go.

What i did was add the java.util.Random class instead of Math.random() for random number generation.

Used an ArrayList that allocates more memory if it needs more, to store the values that are lower or greater than 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 + " ");
    }
}

No new arrays implementation extract:

  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());

What this does is iterate over the array and:

  • count how many number above 500 are there and how many are below 500.
  • build 2 strings with the numbers.

这篇关于打印所有号码设定值之间的一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 23:03