0

JAVA Array of random numbers in range

Hi, guys! I need help from local experts: I made a code that generates random numbers in the array, but I needt it to pick random numbers in some particular range(now it's picking any). I'm trying to figure out how to do it myself, but I have a biger problem that I have a really hard time to resolve: I need it to use a foreach loop to print all elements of the array accurate to four decimal places, and pass the array of doubles to a method that executes as follows: uses a for loop and a Math class method to round up each element to its nearest integer. creates a 10-element array of ints named temp. uses another for loop to cast each element of the double array to temp. returns temp That's what I really need help with. Please, help me to correct it. Any help is much appreciated! Here is my code: https://code.sololearn.com/czRXyUwyqaYT

5th Feb 2018, 12:14 AM
DIY Mods
DIY Mods - avatar
2 Antworten
+ 9
import java.util.Random; public class RandArray { private static Double[] anArray = new Double[10]; private static Random rand = new Random(); public static void list() { for(int i=0;i<anArray.length;++i) anArray[i]= rand.nextInt(10)+rand.nextDouble(); } public static void print() { for(double n: anArray) System.out.printf("%.4f \n", n); } public static int[] copy() { int[] temp = new int[10]; for(int i=0;i<anArray.length;++i) temp[i]= (int)Math.round(anArray[i]); return temp; } public static void main(String args[]) { list(); print(); int[] arr = copy(); for (int i : arr) System.out.println(i); } }
5th Feb 2018, 12:37 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
Thank you so much for the response! How to print sorted numbers in 1 line? I cant manage to accomplish it
8th Feb 2018, 7:31 PM
DIY Mods
DIY Mods - avatar