對於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
你是什麼意思,你是有麻煩的總和?你迄今試過的代碼在哪裏計算總和?或者你只是希望我們爲你做這個部分? – nhouser9
我投票結束這個問題作爲題外話,因爲涉及家庭作業的問題必須顯示一些嘗試自己解決問題的嘗試。 – nhouser9
什麼部分是專門爲你困住的?如果您可以查看您嘗試過的內容,這將有所幫助。它看起來好像你的代碼實際上試圖對列表中的所有項目進行求和。 –