I'm new to Java, and I'm having problems with a math test program I'm creating (no worries; the issue isn't a mathematical calculation). I'm putting an array of doubles that all equal zero through a for loop that'll assign a random number to them. They retain the random number, but then those numbers are reset to zero once it exits the for loop. I thought once I stored the changed number in a loop, it was permanently changed, but is this not the case? Here's the code segment:
int a = 0;
int b = 0;
int c = 0;
double x = 0;
Random num = new Random();
int[] abc = {a, b, c};
for(int i = 0; i <= (abc.length-1); i++){
int nums = num.nextInt(10);
abc[i] = nums;
System.out.println(abc[i]);
}
System.out.println(a + " " + b + " " + c);
I put a system.out in and out of the for loop to see what prints. I'm not quite certain why they all become zeros again, or how I'm supposed to properly revalue the elements. Any help is appreciated. Thank you for your time.
It is working properly,But you are checking wrong variables.You are not changing values of
a
,b
&c
variables you are changing values ofabc
. If you checkabc
it will give you correct values: