简体   繁体   中英

Totaling up from an array converted from a string to an integer(command line compiling)

I am compiling the code in command line with the following code typed in command line:

java aac 2 4 6 8 10

and I am getting the result:

5

The number in position 0 is 2.0
The Sum is: 2.0

The number in position 1 is 4.0
The Sum is: 4.0

The number in position 2 is 6.0
The Sum is: 6.0

The number in position 3 is 8.0
The Sum is: 8.0

The number in position 4 is 10.0
The Sum is: 10.0

What I am trying to achieve is, for the sum to be the total of all the numbers divided by the amount of numbers, however for the amount of numbers there is I have come up with a length variable. For this example the length is displayed as 5 right at the start.

public class aac {
    public static void main(String args[]) {
        // working out the length
        int length = args.length;
        System.out.println(length);

        // this is a for loop that repeats until integer i is greater than
        // integer length, which is the length of the args String array.
        for (int i = 0; i < length; i++) {
            // this string equals whatever value is in position i in string array args
            String all = args[i];
            // integer numConvert now equals the integer of String all
            double numConvert = Double.parseDouble(all);
            System.out.print("The number in position " + i + " is " + " ");
            System.out.println(numConvert);
            double sum = 0;
            sum = sum += numConvert;
            System.out.println("The Sum is: " + sum);
            System.out.println();
        }
    }
}

Are you having problems creating the sum in order to calculate the average? If so, move the double sum = 0; out of your for loop. After the loop you divide it by args.length and that'll be your average.

Here's a little amelioration of your code :

double average = 0.0;
double sum = 0;

for(int i = 0; i < length; i++){
    String all = args2[i];
    double numConvert = Double.parseDouble(all);
    System.out.print("The number in position "+i+" is ");
    System.out.println(numConvert);
    sum += numConvert;
    average = sum / (i+1);
    System.out.println("The Sum is: "+sum);
    System.out.println("The average is :" + average);
    System.out.println();
}

I created 2 double variables outside of your for loop.

Each time we loop in, the current value is added to the sum variable to get the total sum.

Also, the average is changed to the value of sum divided by the numbers we've already seen.

Here is the output :

5
The number in position 0 is 2.0
The Sum is: 2.0
The average is :2.0

The number in position 1 is 4.0
The Sum is: 6.0
The average is :3.0

The number in position 2 is 6.0
The Sum is: 12.0
The average is :4.0

The number in position 3 is 8.0
The Sum is: 20.0
The average is :5.0

The number in position 4 is 10.0
The Sum is: 30.0
The average is :6.0

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