2016-10-03 31 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

我沒有看到,但即使我看着它,並意識到你不能在浮動轉換雙打? –

相關問題