计算时忽略 Java 数组列表中的负值

Ignoring of negative values from Java Arraylist for calculations

我的代码的想法是,它询问用户每个月的收入,直到用户输入负值,负值不应该添加到总收入中,应该显示在输出中,但在计算中被忽略。 然后代码计算总收入(忽略最后一个负值)、平均收入(忽略负值)和所有值的biggest/maximum值。我没有问题得到正确的最大值。但是我怎么能忽略计算中的负收益,甚至根本不将其添加到数组中呢?

问题在于,该计算还在总和和平均收入计算中添加了负值 value/income。而且不忽略负收入的月份

到目前为止,这是我的代码:

package income;

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;

public class Income {

    public static void main(String[] args) {
        int sum = 0;
        int months = 0;
        Scanner input = new Scanner(System.in);
        System.out.println("Write the income of each month.");

         ArrayList<Integer> array = new ArrayList<Integer>();
         System.out.println("Write the income of month 1: ");
            int income = input.nextInt();
            sum += income;
            months++;
            array.add(income);

        while(true){
            months++;
            System.out.println("Write " + months + ". month income: ");
            if(income >= 0){
            income = input.nextInt();
            sum += income;

            array.add(income);

            }
            else if (income < 0){
                break;
            }
        }

        /*This did not work
          for(int i= 0; i < array.size(); i++){
              if(income < 0){
                  array.remove(i);
              }
          }*/
        months--;

        System.out.println("The total income is " + sum);
        System.out.println("The average income is " + sum/months);

        //This one below works fine
        System.out.println("The biggest income is " + Collections.max(array));
        } 
    }

虽然您确实在计算中添加了最后一个负数,但这并不是您的代码无法运行的最终原因。您实际上是在检查您读取的 previous 输入是否大于 0:

while(true){
    months++;
    System.out.println("Write " + months + ". month income: ");
    if(income >= 0){ <------
    income = input.nextInt();

所以循环只会在 previous 输入小于 0 时停止。换句话说,当你输入例如-1 中,直到循环的下一次迭代才检查输入,此时 -1 已经添加到数组中。因此,您应该在 nextInt:

之后立即检查 income >= 0
System.out.println("Write the income of each month.");
ArrayList<Integer> array = new ArrayList<>();
while(true){
    months++;
    System.out.println("Write the income for month" + months + ":");
    int income = input.nextInt();
    if(income >= 0){
        sum += income;

        array.add(income);

    }
    else {
        break;
    }
}

请注意,我还删除了 Write the income of each month.while 循环之间的位,因为它是多余的。

我想这就是您要找的。

    var list = new ArrayList<Integer>();
    var in = new Scanner(System.in);
    var i = 0;

    System.out.println("Write Income of Each Month.");

    while (true)
    {
        System.out.print("Write " + ++i + ".month income : " );
        int num = in.nextInt();

        if (num < 0)
        {
            int sum = list.stream().mapToInt(Integer::intValue).sum();
            double avg = (double) sum / --i;
            int max = Collections.max(list);

            System.out.printf("Sum : %d\nAverage : %f\nBiggest Number : %d\nNegative No. %d", sum ,avg, max, num);
            break;
        }
        else
        {
            list.add(num);
        }
    }

您可以使用 Integer.signum(int) 函数来了解该值是负数(返回值 = -1)还是零(返回值 = 0)或正数(返回值 = 1)。所以,基本无视if Integer.signum(income) == -1