random numbers in array please help
Code java:
public class Arrays {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x, y =0;
int [] list;
list = new int [10];
int [] list2;
list2 = new int [10];
x =(int)(99*Math.random()+1);
y=(int)(99*Math.random()+1);
int array = 9;
int [] list3;
list3 = new int [10];
int [] list4;
list4 = new int [10];
int i;
System.out.print("List 1 : ");
for (i = 0 ; i<=list.length ; i++)
list [i] = x =(int)(99*Math.random()+1);
System.out.print(list[i]);
i dont really know much yet because its out first time dealing with arrays
i want to input a random value between 1-99 in list[0]-list[9] so i used a for loop is this possible?
i previously have this other code which is really long so i wondered if i could use a for loop please help...
Code java:
list[0] = x;
x =(int)(99*Math.random()+1);
list[1] = x;
x =(int)(99*Math.random()+1);
list[2] = x;
x =(int)(99*Math.random()+1);
list[3] = x;
x =(int)(99*Math.random()+1);
list [4] = x;
x =(int)(99*Math.random()+1);
list [5] = x;
x =(int)(99*Math.random()+1);
list [6] = x;
x =(int)(99*Math.random()+1);
list [7] = x;
x =(int)(99*Math.random()+1);
list [8] = x;
x =(int)(99*Math.random()+1);
list [9] = x;
x =(int)(99*Math.random()+1);
list2[0] = x;
x =(int)(99*Math.random()+1);
list2[1] = x;
x =(int)(99*Math.random()+1);
list2[2] = x;
x =(int)(99*Math.random()+1);
list2[3] = x;
x =(int)(99*Math.random()+1);
list2[4] = x;
x =(int)(99*Math.random()+1);
list2[5] = x;
x =(int)(99*Math.random()+1);
list2[6] = x;
x =(int)(99*Math.random()+1);
list2[7] = x;
x =(int)(99*Math.random()+1);
list2[8] = x;
x =(int)(99*Math.random()+1);
list2[9] = x;
x =(int)(99*Math.random()+1);
Re: random numbers in array please help
For future reference, please surround your code with the [highlight=java][/highlight]
Yes a loop is possible. I see you have a loop in the first snippet, a few changes
Code :
for (i = 0 ; i<=list.length ; i++){
list [i] =(int)(99*Math.random()+1);
System.out.print(list[i]);
}
First note the curly brackets, otherwise the println doesn't know what i is. Second, why assign x for every loop? See The for Statement for more info
Re: random numbers in array please help
thanks so much its working now but i dont get why an error keeps popping..
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at lab8.Arrays.main(Arrays.java:35)
dont get it... so i deleted x and went with your code....
so i have this one
System.out.print("List 1 : ");
for (i = 0 ; i<=list.length ; i++){
list [i] =(int)(99*Math.random()+1);
System.out.print(list[i]+ " ");
}
so after i run that i output
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at lab8.Arrays.main(Arrays.java:35)
List 1 : 31 59 36 63 35 6 53 23 88 40 Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
this so in every box of the array it outputs a random number from 1-99.. so far it works but yeah i dont know whats causing the error..
do i import something?
so far i inputted
import java.util.Scanner;
import java.io.*;
Re: random numbers in array please help
Think about the loop and how arrays are zero based: with an array of size 10, array[10] is out of bounds for the array - an exception will be thrown. See the red in the following:
for (i = 0 ; i<=list.length ; i++){
Once again, please surround your code with the code tags I mentioned above.
Re: random numbers in array please help
o ok overlooked the equal sign sorry....
and thank you.. and uhm 1 more thing, how can i align the arrays?
is there a command like printf for arrays?
Re: random numbers in array please help
First off, you're not printing an array. Each time through the loop, you're printing an array element which happens to be an int.
Also, there's no need to concatenate an empty String. println knows how to handle an int, as well as various other primitive types and Objects. Read the API:PrintStream (Java Platform SE 6)
db