2016-07-21 8 views
0

Ich versuche, eine Konsole-Anwendung zu erstellen, wo ich die Eingabe von dem Benutzer für die gesamte Fläche fragen dann frage ich den Benutzer die Anzahl der Strukturen, die unsere in diesem Bereich und dann ich die Werte speichern in der Liste, was ich versuche t tun, ist die Gesamtfläche von Struktur subtrahieren sind, aber ich kann die Werte aus der ListeWie summiere Werte in ArrayList

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; 

     }*/ 

    } 
} 
+1

_aber ich kann die Werte aus der Liste nicht summieren_ Warum? Was ist los mit dir? Was hindert dich daran? –

+0

Ich weiß nicht, wie ich auf die Werte zugreifen kann, die ich mit einer for-Schleife versuchte, aber ich bin nicht sicher, wo die Werte gespeichert werden @SotiriosDelimanolis –

+0

Eine 'void'-Methode sollte nicht mit' get' beginnen. – shmosel

Antwort

1

Versuchen Sie diese Summe:

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

So hat es funktioniert, was ich getan habe ist doppelte Summe = 0; für (Doppelartikel: rasen.liste) { tot + = item; Summe = tot; } System.out.println ("Summe" + Summe); double areaMinusStructure = totalArea - Summe; System.out.print (BereichMinusStruktur); } –

1

Sie können es tun, indem u sing a java 8 lambda:

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