+ 3

Can someone explain how to create an int list with 5 random numbers from 1-10?

I'm trying to create an int list with 5 random numbers each one up to 10

6th Mar 2017, 4:01 PM
Anthony
Anthony - avatar
13 Respostas
+ 1
public class random { public static void main(String args[]) { int i,n; for(i=1;i<=5;i++)//as you want 5 random no.s { n=(int)((Math.random()*(10-1))+1); System.out.println(n); } } }
9th Dec 2018, 11:01 AM
mn121
mn121 - avatar
0
import java.util.*; Random rnd=new Random(); int[] arr=new int[5]; for(int i=0;i<5;i++) arr[i] = rnd.nextInt(10) +1; List<Integer> lt= new ArrayList<Integer>(); for(int z:arr) lt.add(z);
6th Mar 2017, 4:08 PM
Meharban Singh
Meharban Singh - avatar
0
When I run that im getting an error message saying "RandomList.java:12:error: unexpected type List<int> It=new ArrayList<int>(); ^ Required: reference Found: int RandomList.java:12 error: type List does not take parameters
6th Mar 2017, 4:25 PM
Anthony
Anthony - avatar
0
It does not work in Sololearn bcoz it does not have List libraries
6th Mar 2017, 4:46 PM
Meharban Singh
Meharban Singh - avatar
0
It does not work in Sololearn bcoz it does not have Collections
6th Mar 2017, 4:47 PM
Meharban Singh
Meharban Singh - avatar
0
Im using it in Jgrasp and it still is not working
6th Mar 2017, 4:47 PM
Anthony
Anthony - avatar
0
Its working fine for me on eclipse.
6th Mar 2017, 5:01 PM
Meharban Singh
Meharban Singh - avatar
0
Hmm, its still giving me the same error message about List<Interger> not taking parameters. Thanks anyway
6th Mar 2017, 5:07 PM
Anthony
Anthony - avatar
0
Have you imported collections and random class?
6th Mar 2017, 5:55 PM
Meharban Singh
Meharban Singh - avatar
0
Im new to this, so im not really sure. I imported all in java util with the sentex "import java.util*;" Is this not how you import these classes as well? I thought if you use the asterisk, you're importing these classes as well.
7th Mar 2017, 3:13 AM
Anthony
Anthony - avatar
0
yes, import java.util.*;
7th Mar 2017, 3:37 AM
Meharban Singh
Meharban Singh - avatar
0
Yea i used that but was still unsuccessfull
7th Mar 2017, 3:38 AM
Anthony
Anthony - avatar
0
@Anthony, There are 2 types of list. import java.awt.List; and import java.util.List; You need second one. So use, import java.util.List; import java.util.Random;
7th Mar 2017, 4:22 AM
Meharban Singh
Meharban Singh - avatar