java - 如何对 arrayList 中的值求和

标签 java arrays list arraylist

I am trying to create a console application where i take input from the user asking for total area then i ask the user the amount of structures that our in that area and then I store the values in the list what I am trying t do is substract the total area by structure are but i cant sum the values from the List

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 (Double item : lawn.list) {
    tot += item;
}

关于java - 如何对 arrayList 中的值求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38492987/

相关文章:

java - Calendar.AM 必须是 : Calendar.(工作日)之一

java - 如何使用 java 以编程方式清除 Lotus 数据库中的所有复制选项

arrays - 为什么 pipe equals 不适用于 Ruby 中的 tap?

python - 根据第三个列表中的随机项目,从两个列表之一中选择一个随机项目

r - 索引操作删除属性

python - 效率 : 2D-list to dictionary in python

java - 确定平均值

java - PMD "Bean Members Should Serialize"规则。我们可以用更聪明的方式来做吗?

javascript - 在 React useState Hook 中删除数组的第一个元素?

c++ - char*与char[]的区别(二)