Fill an array with random numbers

后端 未结 13 2218
感动是毒
感动是毒 2020-12-03 11:54

I need to create an array using a constructor, add a method to print the array as a sequence and a method to fill the array with random numbers of the type double.

He

相关标签:
13条回答
  • 2020-12-03 12:26

    This seems a little bit like homework. So I'll give you some hints. The good news is that you're almost there! You've done most of the hard work already!

    • Think about a construct that can help you iterate over the array. Is there some sort of construct (a loop perhaps?) that you can use to iterate over each location in the array?
    • Within this construct, for each iteration of the loop, you will assign the value returned by randomFill() to the current location of the array.

    Note: Your array is double, but you are returning ints from randomFill. So there's something you need to fix there.

    0 讨论(0)
  • 2020-12-03 12:30

    This will give you an array with 50 random numbers and display the smallest number in the array. I did it for an assignment in my programming class.

    public static void main(String args[]) {
        // TODO Auto-generated method stub
    
        int i;
        int[] array = new int[50];
        for(i = 0; i <  array.length; i++) {
            array[i] = (int)(Math.random() * 100);
            System.out.print(array[i] + "  ");
    
            int smallest = array[0];
    
            for (i=1; i<array.length; i++)
            {
            if (array[i]<smallest)
            smallest = array[i];
            }
    
    
    
        }
    
    }
    

    }`

    0 讨论(0)
  • 2020-12-03 12:31

    You could loop through the array and in each iteration call the randomFill method. Check it out:

    import java.util.Random;
    
    public class NumberList {
        private static double[] anArray;
    
        public static double[] list() {  
            return new double[10];
        }
    
        public static void print() {
            System.out.println(String.join(" ", anArray));
        }
    
    
        public static double randomFill() {
            return (new Random()).nextInt();
        }
    
        public static void main(String args[]) {
            list();
            for(int i = 0; i < anArray.length; i++)
                anArray[i] = randomFill();
    
            print();
        }
    }
    
    0 讨论(0)
  • 2020-12-03 12:33

    People don't see the nice cool Stream producers all over the Java libs.

    public static double[] list(){
        return new Random().ints().asDoubleStream().toArray();
    }
    
    0 讨论(0)
  • 2020-12-03 12:33

    Fast and Easy

    double[] anArray = new Random().doubles(10).toArray();
    
    0 讨论(0)
  • 2020-12-03 12:34

    You need to add logic to assign random values to double[] array using randomFill method.

    Change

     public static double[] list(){
        anArray = new double[10];   
        return anArray;
     }
    

    To

     public static double[] list() {
        anArray = new double[10];
        for(int i=0;i<anArray.length;i++)
        {
            anArray[i] = randomFill();
        }
        return anArray;
    }
    

    Then you can call methods, including list() and print() in main method to generate random double values and print the double[] array in console.

     public static void main(String args[]) {
    
    list();
    print();
     }
    

    One result is as follows:

    -2.89783865E8 
    1.605018025E9 
    -1.55668528E9 
    -1.589135498E9 
    -6.33159518E8 
    -1.038278095E9 
    -4.2632203E8 
    1.310182951E9 
    1.350639892E9 
    6.7543543E7 
    
    0 讨论(0)
提交回复
热议问题