简体   繁体   中英

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:

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. 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

The second change is to your summation logic. Lets assume your array contains the three numbers [1, 2, 3] and walk through your logic.

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.
ie j = 2, k = 2+1, sum = 2 + 2 + 1. followed by 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. In my updated logic, we simply loop through each element and add it to the current running total.

hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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