Selecting an array value with a variable
Hey,
Im fairly new to Java, and im doing a course in it at the moment. I have been set some homework to do, but ive got a little stuck :( ...what i have to do is create a program that randomly generates numbers between 1 and 10, and then i have made an array which has 10 values, and say number 1 comes up in the random number generation, i want array [1] to have the number of times 1 appears in it. Ive came up with the following, but i keep getting errors:rolleyes: ...i think there is a problem with defineing the variable i as a long, then the array as an integer...
Code:
int[] ar = {0,0,0,0,0,0,0,0,0,0};
int a = 0;
while (a<10)
{
a++;
long i = Math.round(Math.random()*10);
ar[i] = i;
}
System.out.println(ar);
with that code i get these errors:
Code:
found : long
required: int
ar[i] = i;
^
C:\Website Projects\Java Work\test 1\Document.java:14: possible loss of precision
found : long
required: int
ar[i] = i;
if anyone could help me get this working i would be most greatful:)
Thanks for your time,
-Ross
[ArchAngel added CODE tags]