简体   繁体   English

如何在Java中对整数输入求和,并在程序结束时在数组中显示结果?

[英]How to sum integer inputs in Java, and display the results in an array at the end of the program?

I am trying to create a "calculator", except it needs to print each element that was used to compose the sum total. 我正在尝试创建一个“计算器”,除了它需要打印用于组成总和的每个元素。 This printing of the array elements needs to happen at the end of the program, when the user inputs 0 twice in a row. 当用户连续两次输入0时,阵列元素的这种打印需要在程序结束时发生。

Upon entering an input, the integer values will be stored in an array. 输入输入后,整数值将存储在数组中。 Once the end of the program has been reached, the contents of this array will be printed. 一旦到达程序结束,将打印该数组的内容。 However, if the end of the program has not been reached, the program continues while the user adds consecutive inputs. 但是,如果尚未到达程序结束,则程序会在用户添加连续输入时继续。

Currently, the program will only print one element at a time, instead of every element that was used to calculate the total. 目前,程序一次只打印一个元素,而不是用于计算总数的每个元素。 I've spent hours trying to debug, and any guidance would be greatly appreciated! 我花了好几个小时试图调试,任何指导都将非常感谢!

import java.util.*;

public class AddingMachine {

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    boolean justStarting = true;
    int total = 0;
    int subtotal = 0;
    int input;
    int last = 1;
    int MAXIMUM_NUMBER_OF_INPUTS = 100;
    while (true) {
        input = scanner.nextInt();
        if (input == 0) {
            if (last == 0) {
                System.out.println("total " + total);
                return;
            }
            System.out.println("subtotal " + subtotal);
            total += subtotal;
            subtotal = 0;
        }
        subtotal += input;
        last = input;
        int[] numbers = new int[args.length];
        for (int i = 0; i < args.length; i++) {
            numbers[i] = last;
        }
        System.out.println(Arrays.toString(numbers));
    }
}

When summing the input in your loop, you could store the users input into a List of integers. 在对循环中的输入求和时,可以将用户输入存储到整数列表中。 Once you need to reprint them, you can iterate over the List and print the elements you stored. 一旦需要重新打印它们,就可以遍历List并打印存储的元素。

Example: 例:

List<Integer> storedUserInput = new ArrayList<>();
while (true) {
        input = scanner.nextInt();
        storedUserInput.add(input);
    if (input == 0) {
        if (last == 0) {
            for(Integer i : storedUserInput){
                 System.out.print(i + " + ");
            }
            System.out.println("total " + total);
            return;
        }
        System.out.println("subtotal " + subtotal);
        total += subtotal;
        subtotal = 0;
    }

}

Within the while loop, the array is re-initialized each time: while循环中,每次都重新初始化数组:

int[] numbers = new int[args.length];

so any previously entered value is lost. 所以以前输入的任何值都会丢失。 Also, the purpose of the for loop within the while is not clear. 此外,的目的for的内循环while目前尚不清楚。

Also, unless using an array is a requirement, you really don't need an array. 此外,除非使用数组是必需的,否则您实际上不需要数组。 You could just use a StringBuffer and append the entered values. 您可以使用StringBuffer并附加输入的值。

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

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