忽略Java Arraylist中的负值进行计算

忽略Java Arraylist中的负值进行计算,java,arraylist,negative-number,Java,Arraylist,Negative Number,我的代码的想法是,它要求用户每月的收入,直到用户输入负值,该负值不应添加到总收入中,应显示在输出中,但在计算中忽略。 然后,代码计算总收入(忽略最后一个负值)、平均收入(忽略负值)和所有值中的最大值。我没有问题得到正确的最大值。但我怎么能在计算中忽略负收入,甚至不将其添加到数组中呢 问题在于,计算还将负值/收入添加到总金额和平均收入计算中。它并没有忽略负收入的月份 以下是我目前的代码: package income; import java.util.Scanner; import java.

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

问题在于,计算还将负值/收入添加到总金额和平均收入计算中。它并没有忽略负收入的月份

以下是我目前的代码:

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));
        } 
    }
套餐收入;
导入java.util.Scanner;
导入java.util.ArrayList;
导入java.util.Collections;
公共阶层收入{
公共静态void main(字符串[]args){
整数和=0;
整月=0;
扫描仪输入=新扫描仪(System.in);
System.out.println(“写出每个月的收入”);
ArrayList数组=新的ArrayList();
System.out.println(“写出第1个月的收入:”);
int income=input.nextInt();
总和+=收入;
月++;
数组。添加(收入);
while(true){
月++;
System.out.println(“写入”+月+”。月收入:);
如果(收入>=0){
收入=投入。nextInt();
总和+=收入;
数组。添加(收入);
}
否则,如果(收入<0){
打破
}
}
/*这不起作用
对于(int i=0;i
虽然您确实在计算中加入了最后一个负数,但这并不是代码无法工作的最终原因。实际上,您正在检查之前读取的输入是否大于0:

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

请注意,我还删除了
写每月收入。
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);
        }
    }
var list=new ArrayList();
var in=新扫描仪(System.in);
var i=0;
System.out.println(“写入每个月的收入”);
while(true)
{
系统输出打印(“写入”++i+”。月收入:);
int num=in.nextInt();
if(num<0)
{
int sum=list.stream().mapToInt(Integer::intValue.sum();
双平均值=(双)和/--i;
int max=Collections.max(列表);
System.out.printf(“总和:%d\n平均值:%f\n最大值:%d\n否定号%d”,总和,平均值,最大值,数值);
打破
}
其他的
{
列表。添加(num);
}
}

您可以使用该函数了解值是负值(返回值=-1)还是零(返回值=0)或正值(返回值=1)。所以,基本上忽略
如果Integer.signum(income)=-1

为什么还要将最后一个负值添加到列表中?其思想是,当我添加负值时,它不再要求更多值,并计算和打印这些内容:总收入,平均收入和最高收入。计算时需要负值吗?我不想将负值添加到列表中,但不知道如何成功避免。我需要数组列表来使用集合框架。我根本不需要负值来计算。如果你不关心负值,除了作为一个止动器,你只需要反转If条件,而不将值添加到列表中。
    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);
        }
    }