2016-10-03 74 views
0

对于CS类中的一个赋值,我不得不接受一个浮点数的输入,将它们保存在一个数组中,然后显示它们并将它们加在一起得到花车的总和。目前我遇到了获取花车总和的问题。在一个数组中输入Floats然后将它们加在一起

据我可以告诉下面的代码应该工作,但我得到一个错误:“不能添加一个对象和一个int”。

我的代码是:

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Comparator; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Scanner; // load the scanner utility 

class Lab7 { 

    public static void main(String[] args) { 

     double n; 
     double s; 
     Scanner input = new Scanner(System.in); //define the scanner 

     List L = new ArrayList(); 
     s=0.0; 
     n=1.0; 
     // read in the floats 
     while (n != 0.0) 
      { 
      System.out.println("Please input a number"); 
      n = input.nextFloat(); 
      if (n != 0.0) L.add(n); 
      System.out.println("read in " + n); 

      } 

     for (int i=0; i< L.size(); i= i+1) 
     { 
      System.out.println("List contains " + L.get(i)); 
      s = s + L.get(i); 
      System.outprintln("Sum of nunbers " + s); 
     } 



    }// of main 

    } // of Lab7 
+0

你是什么意思,你是有麻烦的总和?你迄今试过的代码在哪里计算总和?或者你只是希望我们为你做这个部分? – nhouser9

+0

我投票结束这个问题作为题外话,因为涉及家庭作业的问题必须显示一些尝试自己解决问题的尝试。 – nhouser9

+0

什么部分是专门为你困住的?如果您可以查看您尝试过的内容,这将有所帮助。它看起来好像你的代码实际上试图对列表中的所有项目进行求和。 –

回答

0

我有固定的代码,把下面的新版本。

主要问题是您使用ArrayList的方式。你使用的是一种“原始类型”。查看此链接了解为什么那么糟糕:What is a raw type and why shouldn't we use it?。 Basicaly,你应该提供ArrayList一个类型参数,如:ArrayList<String>

但是,在这种情况下,List甚至不需要。当输入每个新号码时,我们可以简单地保留总金额。我改变了代码来代替。

我也重新命名了你的变量。你永远不应该命名为nx。名称变量描述了它们的用途。这让你或者你需要帮助的人(比如StackOverflow上的人)更容易阅读你的代码。让代码易于理解与使其正确工作几乎同等重要,尤其是当您开始处理更大的项目时。

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Comparator; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Scanner; // load the scanner utility 

class Lab7 { 

    public static void main(String[] args) { 

     double currentNumber = 1.0; 
     double sum = 0.0; 
     Scanner input = new Scanner(System.in); //define the scanner 

     // read in the floats 
     while (currentNumber != 0.0) 
     { 
      System.out.println("Please input a number"); 
      currentNumber = input.nextFloat(); 
      System.out.println("read in " + currentNumber); 
      sum = sum + currentNumber; 
     } 
     System.outprintln("Sum of nunbers " + sum); 

    }// of main 

} // of Lab7 

如果你绝对要使用数组对于此实验室(请记住,它不需要以任何方式,做的唯一原因,如果实验室明确地说,是你必须这样做),你可以使用以下内容:

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Comparator; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Scanner; // load the scanner utility 

class Lab7 { 

    public static void main(String[] args) { 

     double currentNumber = 1.0; 
     double sum = 0.0; 
     Scanner input = new Scanner(System.in); //define the scanner 
     ArrayList<Float> floats = new ArrayList<>(); //notice how we give a type argument 

     // read in the floats 
     while (currentNumber != 0.0) 
     { 
      System.out.println("Please input a number"); 
      currentNumber = input.nextFloat(); 
      System.out.println("read in " + currentNumber); 
      floats.add((float) currentNumber); 
      sum = sum + currentNumber; 
     } 
     System.outprintln("Sum of nunbers " + sum); 

    }// of main 

} // of Lab7 
+0

关于这一点的事情是我需要列表以便稍后找到该组数字的MEAN。 –

+0

@MasonSalcido似乎你可能没有阅读我的整篇文章......我说“如果你绝对必须使用一个数组为这个实验,你可以使用以下”,然后给出第二个例子,其中的数字存储在一个数组。 – nhouser9

+0

我没有看到,但即使我看着它,并意识到你不能在浮动转换双打? –

相关问题