试图让这两个数组将输入总和存储在第三个列表中

Trying to make these 2 arrays store the input sum in a 3rd list

我正在学习 java,我正在努力使这件事发挥作用。

我在 java 中制作了 3 个数组,其中 2 个接受输入,每个数组有 10 个元素。

list1和list2的内容之和应该存储在list 3中,并显示结果

这是我的代码(真的很乱,抱歉)

public class List2 {
    public static void main(String[]args){
    int list1[]=new int[10];
    int list2[]=new int[10];
    int list3[]=new int[10];
    int i, sum=0, num1=0, num2=0;
    String input=" ";

    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

    for(i=0;i<10;i++){
            list1[i]=0;
            list2[i]=0;
            list3[i]=0;
        }
    for(i=0;i<10;i++){
            System.out.print("Input value for list1[" + i + "] = ");

            try{
                input=in.readLine();
            }catch(IOException e){}
            num1=Integer.parseInt(input);
            list1[i]=num1;
           }

    for(i=0;i<10;i++){
            System.out.println("list[" + i + "] = "+list1[i]);
    }
    for(i=0;i<10;i++){
            System.out.print("Input value for list2[" + i + "] = ");

            try{
                input=in.readLine();
            }catch(IOException e){}
            num2=Integer.parseInt(input);
            list2[i]=num2;
           }
    for(i=0;i<10;i++){
            System.out.println("list[" + i + "] = "+list2[i]);
    }    
    for(i=0;i<10;i++){
        sum = list1[i]+list2[i];
    }
    for(i=0;i<10;i++){
        System.out.println("list3[" + sum + "]="+"list1[" + list1[i] + "]+"+"list2[" + list2[i] + "]");
    }
    }
}

令我沮丧的是,在整个 list3 中只显示了 list1 和 list2 的最后输入。这些是结果

**Input value for list1[0] = 1
Input value for list1[1] = 2
Input value for list1[2] = 3
Input value for list1[3] = 4
Input value for list1[4] = 1
Input value for list1[5] = 2
Input value for list1[6] = 3
Input value for list1[7] = 1
Input value for list1[8] = 41
Input value for list1[9] = 2
list[0] = 1
list[1] = 2
list[2] = 3
list[3] = 4
list[4] = 1
list[5] = 2
list[6] = 3
list[7] = 1
list[8] = 41
list[9] = 2
Input value for list2[0] = 4
Input value for list2[1] = 1
Input value for list2[2] = 2
Input value for list2[3] = 1
Input value for list2[4] = 1
Input value for list2[5] = 3
Input value for list2[6] = 1
Input value for list2[7] = 1
Input value for list2[8] = 1
Input value for list2[9] = 1
list[0] = 4
list[1] = 1
list[2] = 2
list[3] = 1
list[4] = 1
list[5] = 3
list[6] = 1
list[7] = 1
list[8] = 1
list[9] = 1
list3[3]=list1[1]+list2[4]
list3[3]=list1[2]+list2[1]
list3[3]=list1[3]+list2[2]
list3[3]=list1[4]+list2[1]
list3[3]=list1[1]+list2[1]
list3[3]=list1[2]+list2[3]
list3[3]=list1[3]+list2[1]
list3[3]=list1[1]+list2[1]
list3[3]=list1[41]+list2[1]
list3[3]=list1[2]+list2[1]

有人知道我应该怎么做才能使其正常工作吗?

您需要添加每个索引处的值并将它们放回列表3。

    for (int i = 0; i < 10; i++) {
        list3[i] = list1[i] + list2[i];
    }

我想这就是你想要达到的目标..

import java.util.*;
import java.io.*;
public class fib{
    public static void main(String[]args){
    int list1[]=new int[10];
    int list2[]=new int[10];
    int list3[]=new int[10];
    int i, sum=0, num1=0, num2=0;
    String input=" ";

    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

    for(i=0;i<10;i++){
            list1[i]=0;
            list2[i]=0;
            list3[i]=0;
        }
    for(i=0;i<10;i++){
            System.out.print("Input value for list1[" + i + "] = ");

            try{
                input=in.readLine();
            }catch(IOException e){}
            num1=Integer.parseInt(input);
            list1[i]=num1;
           }

    for(i=0;i<10;i++){
            System.out.println("list[" + i + "] = "+list1[i]);
    }
    for(i=0;i<10;i++){
            System.out.print("Input value for list2[" + i + "] = ");

            try{
                input=in.readLine();
            }catch(IOException e){}
            num2=Integer.parseInt(input);
            list2[i]=num2;
           }
    for(i=0;i<10;i++){
            System.out.println("list[" + i + "] = "+list2[i]);
    }    
    for(i=0;i<10;i++){
        System.out.println( list1[i]+list2[i] + "="+"list1[" + list1[i] + "]+"+"list2[" + list2[i] + "]");
    }
    }
}

'sum' 不是数组。只是一个存储单个值的变量。您正在添加列表 1 和列表 2 的每个元素,但替换为变量 'sum'。相反,要么使 sum 成为一个数组,要么使用列表 3 来存储所有 10 个值

int sum[] = new int[10];
sum[i] = list1[i] + list2[i];

list3[i] = list1[i] + list2[i];

您已经添加了 list1 和 list2 并将其存储在 sum 中,但是您将该总和存储在 list3 中的什么位置?您需要像这样在第三个列表中存储两个列表的总和:

for(i=0;i<10;i++){
    list3[i] = list1[i]+list2[i];
}

然后在最后的 println 语句中,我不太确定您要显示什么。要显示 list3 的内容,只需重复您之前所做的:

   for(i=0;i<10;i++){
            System.out.println("list[" + i + "] = "+list3[i]);
    }