等号两边的值有什么区别?
What difference does it make what values you put on either side of a equals sign?
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int size = input.nextInt();
int[] grades = new int[size];
for(int i=0; i < size; i++){
int x = input.nextInt();
grades[i] = x; **<<<<<< When i use this one i get the correct answer and i can input some numbers and they show up in a array**
x = grades[i]; **<<<<<< However here i input my numbers but the array shows up as zeros only. Why?**
}
System.out.println(Arrays.toString(grades));
}
=
不是数学上的等式,而是 赋值。
在Java中,A = B
表示“将B
的值赋予A
”。
这不是对称操作,因为你之后改变了 A
的值,但没有改变 B
。
因此,grades[i] = x;
将您刚刚从扫描仪读取的值分配给数组的一个元素;但是 x = grades[i]
正在用存储在该数组元素中的任何内容覆盖您刚刚读取的值。
默认情况下,Java 数组用零填充;如果您不为元素分配不同的值,这就是您要打印的内容。
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int size = input.nextInt();
int[] grades = new int[size];
for(int i=0; i < size; i++){
int x = input.nextInt();
grades[i] = x; **<<<<<< When i use this one i get the correct answer and i can input some numbers and they show up in a array**
x = grades[i]; **<<<<<< However here i input my numbers but the array shows up as zeros only. Why?**
}
System.out.println(Arrays.toString(grades));
}
=
不是数学上的等式,而是 赋值。
在Java中,A = B
表示“将B
的值赋予A
”。
这不是对称操作,因为你之后改变了 A
的值,但没有改变 B
。
因此,grades[i] = x;
将您刚刚从扫描仪读取的值分配给数组的一个元素;但是 x = grades[i]
正在用存储在该数组元素中的任何内容覆盖您刚刚读取的值。
默认情况下,Java 数组用零填充;如果您不为元素分配不同的值,这就是您要打印的内容。