【问题标题】:How to sum values in arrayList如何对arrayList中的值求和
【发布时间】:2016-11-24 09:10:42
【问题描述】:

我正在尝试创建一个控制台应用程序,我从用户那里获取输入,询问总面积,然后我询问用户我们在该区域中的结构数量,然后我将值存储在我正在尝试的列表中做的是按结构减去总面积,但我不能总结列表中的值

package lawn;

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

/**
 *
 * @author norbe
 */
public class Lawn {

    private double lenght, width;
    private double totalArea;
    private double structureArea;
    private double tot;
    List<Double> list = new ArrayList<Double>();
    public Lawn (){
    return;}

    public Lawn(double Lenght, double Width) {
        this.lenght = Lenght;
        this.width = Width;

    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double Width) {
        this.width = Width;
    }

    public double getLenght() {
        return lenght;
    }

    public void setHeight(double Lenght) {
        this.lenght = Lenght;
    }

    public void getTotalArea() {
        Scanner sc = new Scanner(System.in);
        double lenght;
        double width;
        System.out.println("please enter Total lenght :\t ");
        lenght = sc.nextDouble();
        if (lenght >= 0.0) {
            System.out.println("Please enter width :\t ");
            width = sc.nextDouble();
            if (width > 0.0) {
                totalArea = lenght * width;
                System.out.println("Your total area is :\t " + totalArea);
            } else {
                System.out.println("Invalid entry");
            }
        } else {
            System.out.println("Invalid entry");
        }

    }

    public void getStructureArea() {
        Scanner sc = new Scanner(System.in);
        /*double lenght;
        double width;*/
        System.out.println("please enter Structure lenght :\t ");
        lenght = sc.nextDouble();
        if (lenght >= 0.0) {
            System.out.println("Please enter  Structure width :\t ");
            width = sc.nextDouble();
            if (width > 0.0) {
                structureArea = lenght * width;
                list.add(structureArea);
                System.out.println("Your Structure area is :\t " + structureArea);
            } else {
                System.out.println("Invalid entry");
            }
        } else {
            System.out.println("Invalid entry");
        }

        System.out.println(list);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        double tot = 0;
        Lawn lawn = new Lawn();
        lawn.getTotalArea();
        System.out.println("Please enter number of structures : ");
        Scanner sc = new Scanner(System.in);
        int structures = sc.nextInt();
        for (int count = 1; count <= structures; count++) {
            lawn.getStructureArea();
        }

        double sum = 0;
       /* for (Double item : list) {
            tot += item.structureArea;

        }*/

    }
}

【问题讨论】:

  • 但我无法汇总列表中的值 为什么?怎么了?是什么阻止你这样做?
  • 我不知道如何访问我尝试使用 for 循环的值,但我不确定在哪里存储值@SotiriosDelimanolis
  • void 方法不应以 get 开头。

标签: java arrays list arraylist


【解决方案1】:

您可以使用 java 8 lambda 来做到这一点:

Double total = lawn.list.stream().mapToDouble(i -> i).sum();

【讨论】:

    【解决方案2】:

    试试这个:

    for (Double item : lawn.list) {
        tot += item;
    }
    

    【讨论】:

    • 所以我所做的是双倍总和 = 0;对于(双重项目:草坪.list){ tot += item;总和=总; } System.out.println("sum" + sum);双 areaMinusStructure = 总面积 - 总和; System.out.print(areaMinusStructure); }
    猜你喜欢
    • 2014-01-29
    • 2016-02-25
    • 1970-01-01
    • 2011-08-31
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多