如何将一个数组的内容添加到另一个数组中,等于另一个数组。
double[] array1 = new double[4];
double[] array2 = new double[4];
double[] array3 = new double[4];
double input;
double number1;
double number2;
System.out.println("Welcome. Please enter the first number in 1st set: ");
// add 4 numbers into 1st array
System.out.println("Please enter the first number in the 2nd set: ");
// add 4 numbers into 2nd array
for (int i = 0; i < array3.length; i++){
array3[i] = array1[i] + array2[i];
System.out.println(" #" + (i+1) + " in the array is " + array3[i]);
}
我不知道为什么底部的代码不起作用。没有出现错误消息,代码中只有0。
我应该这样做:
int number1 = array1[1];
int number2 = array[1];
int finalNumber = number1 + number2;
finalNumber = array3[1];
这似乎不必要地复杂化。
修改的
这是我用来在数组中分配变量的代码。
System.out.println("Welcome. Please enter the first number in 1st set: ");
for (int i = 0; i < array1.length; i++){
System.out.println(i+1 + ": ");
input = TextIO.getlnDouble(); // Retrieves input
input = array1[i];
}
System.out.println("Please enter the first number in the 2nd set: ");
for (int i = 0; i < array2.length; i++){
System.out.println("Number: ");
input = TextIO.getlnDouble(); // Retrieves input
input = array2[i];
}
答案 0 :(得分:0)
您错误地分配值。
您应该使用array1[i]=input;
代替input = array1[i];
。请参阅下文。
for (int i = 0; i < array1.length; i++){
System.out.println(i+1 + ": ");
input = TextIO.getlnDouble(); // Retrieves input
array1[i] = input;
}
System.out.println("Please enter the first number in the 2nd set: ");
for (int i = 0; i < array2.length; i++){
System.out.println("Number: ");
input = TextIO.getlnDouble(); // Retrieves input
array2[i] = input;
}
答案 1 :(得分:0)
你的代码很整洁,看起来很好。
您是否打印并检查前两个数组的值?
因为你已经有了
行double [] array3 = new double [4];
你的for循环没有问题。只需打印前两个数组。
答案 2 :(得分:0)
public static void main (String[] args){
double[] array1 = new double[4];
double[] array2 = new double[4];
double[] array3 = new double[4];
double input;
double number1;
double number2;
System.out.println("Welcome. Please enter the first number in 1st set: ");
array1[0] = 1;
array1[1] = 2;
array1[2] = 3;
array1[3] = 4;
for (int i = 0; i < array2.length; i++){
System.out.println("Number: ");
input = TextIO.getlnDouble(); // Retrieves input
//input = array2[i]; <--- change this to
array2[i] = input;
}
for (int i = 0; i < array3.length; i++){
array3[i] = array1[i] + array2[i];
System.out.println(" #" + (i+1) + " in the array is " + array3[i]);
}
}
我的输出是
Welcome. Please enter the first number in 1st set:
Please enter the first number in the 2nd set:
#1 in the array is 2.0
#2 in the array is 4.0
#3 in the array is 6.0
#4 in the array is 8.0
也许有帮助=)
答案 3 :(得分:0)
你必须改变你的代码
for (int i = 0; i < array2.length; i++){
System.out.println("Number: ");
input = TextIO.getlnDouble(); // Retrieves input
input = array2[i]; <--- change this to
}
那是正确的
for (int i = 0; i < array2.length; i++){
System.out.println("Number: ");
input = TextIO.getlnDouble(); // Retrieves input
array2[i] = input;
}
也是第一个数组
答案 4 :(得分:0)
您错误地将值分配给数组。
要将值分配给的变量位于=
运算符的左侧。您从分配值的变量位于右侧。
目前,您要将array1[i]
中的值指定给input
。
由于您要将input
中的值分配给数组中的某个位置,因此需要更改
input = array1[i];
到
array1[i] = input;
同样使用第二个阵列。