简体   繁体   English

添加通过用户输入决定的数组元素

[英]Adding elements of an array that was decided through user input

I'm trying to add all the elements together in an array that was decided through user input, Every time I run the code that I've constructed below I get a number that is obviously not the sum of the elements.我试图将所有元素添加到一个通过用户输入决定的数组中,每次运行我在下面构建的代码时,我都会得到一个显然不是元素总和的数字。 What am I doing wrong?我究竟做错了什么?

import java.util.Scanner;
public class SumProduct
{
    public static void main (String []args)
    {
        Scanner input = new Scanner (System.in);
        int[] array1 = new int [input.nextInt()];
        input = scan.nextInt();        
        for (int i = 0; i < array1.length; i++)
        {
            array1[i] = input.nextInt();
        }
        for (int i = 0; i < array1.length; i++)
        {
            int j = array1[i];
            int k = array1[i]+1;
            int sum = j + k;
            System.out.print(sum);
        }
    }
}

You probably want to prompt the user to enter the size of the array if you're going to do this.This line of code is allowing whatever the user enters to be the size of your array.如果您打算这样做,您可能希望提示用户输入数组的大小。这行代码允许用户输入的任何内容都是数组的大小。

int[] array1 = new int [input.nextInt()]; //this sets the size of the array through user input

scan doesn't exist in the currrent context:当前上下文中不存在扫描:

input = scan.nextInt();   // this is invalid syntax as scan is not the Scanner you created    
for (int i = 0; i < array1.length; i++)
{
    array1[i] = input.nextInt();
}

I would do this to keep adding elements to the array:我会这样做以继续向数组添加元素:

// no need for this: input = scan.nextInt();
for (int i = 0; i < array1.length; i++)
{
    System.out.println("Enter integer to add:");
    array1[i] = input.nextInt();
}

This code will give you the sum of the elements if you just add one element at a time instead of two to the sum variable:如果您一次只向 sum 变量添加一个元素而不是两个元素,则此代码将为您提供元素的总和:

int sum = 0;
for (int i = 0; i < array1.length; i++)
{
    sum += array1[i];
    System.out.print(sum);  // this will print out sum after each addition
}
System.out.print(sum);  // this will print out sum after the entire array is summed

Adding some logic to only allow the user to enter so many numbers to the array would be helpful as well.添加一些逻辑以仅允许用户向数组输入如此多的数字也会有所帮助。 You will need to move on from them entering data into the array at some point.您将需要从他们在某个时候将数据输入到数组中继续前进。 Then also remember to close the scanner when you're finished getting data from the user:然后还记得在完成从用户那里获取数据后关闭扫描仪:

input.close();

Your problem is in the following lines of code:您的问题出在以下代码行中:

for (int i = 0; i < array1.length; i++)
    {
        int j = array1[i];
        int k = array1[i]+1;
        int sum = j + k;
        System.out.print(sum);
    }

it should look something like它应该看起来像

int sum = 0;
for (int i = 0; i < array1.length; i++)
    {
        sum = sum + array1[i];
    }
System.out.print(sum);

The first change is to declare the variable "sum" outside of the loop.第一个变化是在循环之外声明变量“sum”。 The way it's written, it will get declared, then disappear, then declared, then disappear for every loop iteration.它的编写方式将被声明,然后消失,然后声明,然后在每次循环迭代中消失。 You also probably want to initialize it to 0您可能还想将其初始化为 0

The second change is to your summation logic.第二个变化是你的求和逻辑。 Lets assume your array contains the three numbers [1, 2, 3] and walk through your logic.让我们假设您的数组包含三个数字 [1, 2, 3] 并遍历您的逻辑。

j = array1[0]     //(j == 1)
k = array1[0] + 1 //(k == 1 + 1 == 2)
sum = j + k       //(sum == 1 + 2 == 3)

You then throw out the variable "sum" like I mentioned earlier, and start over with the same logic on the second array element, then again on the third.然后像我之前提到的那样丢弃变量“sum”,在第二个数组元素上使用相同的逻辑重新开始,然后在第三个数组元素上再次开始。
ie j = 2, k = 2+1, sum = 2 + 2 + 1. followed by j = 3, k = 3 + 1, sum = 3 + 3 + 1.即 j = 2, k = 2+1, sum = 2 + 2 + 1. 其次是 j = 3, k = 3 + 1, sum = 3 + 3 + 1。

You can probably see how this isn't the sum, and results in you logging the three digits 3, 5, and 7 for this example case.您可能会看到这不是总和,并导致您在此示例案例中记录三个数字 3、5 和 7。 In my updated logic, we simply loop through each element and add it to the current running total.在我更新的逻辑中,我们简单地遍历每个元素并将其添加到当前运行总数中。

hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM