在下面的節目,我已經編寫一個程序,讀取的5名學生,爲每個5個測驗每個學生標記沿。我有loades在String類型的ArrayList的名稱和標誌競猜型雙的ArrayList。不過,我需要這些測驗標記加載到一個整數,我不知道如何改變這種從雙ArrayList中更改爲整數的ArrayList
import java.util.Scanner;
import java.util.ArrayList;
public class onemoretime
{
public static final double MAX_SCORE = 15 ;
public static final int NAMELIMIT = 5;
public static void main(String[] args)
{
ArrayList<String> names = new ArrayList<>();
ArrayList<Double> averages = new ArrayList<>(); //line that needs to become a Integer ArrayList
Scanner in = new Scanner(System.in);
for (int i = 0; i < NAMELIMIT; i++)
{
String line = in.nextLine();
String[] words = line.split(" ");
String name = words[0] + " " + words[1];
double average = findAverage(words[2], words[3], words[4], words[5], words[6]);
System.out.println("Name: " + name + " Quiz Avg: " + average);
names.add(name);
averages.add(average);
}
}
public static double findAverage(String a, String b, String c, String d, String e)
{
double sum = Double.parseDouble(a) + Double.parseDouble(b) + Double.parseDouble(c) + Double.parseDouble(d) + Double.parseDouble(e);
return (sum/NAMELIMIT);
}
}
對於輸入:
Sally Mae 90 80 45 60 75
Charlotte Tea 60 75 80 90 70
Oliver Cats 55 65 76 90 80
Milo Peet 90 95 85 75 80
Gavin Brown 45 65 75 55 80
我得到正確的輸出
Name: Sally Mae Quiz Avg: 70.0
Name: Charlotte Tea Quiz Avg: 75.0
Name: Oliver Cats Quiz Avg: 73.2
Name: Milo Peet Quiz Avg: 85.0
Name: Gavin Brown Quiz Avg: 64.0
所以要整輸出,但是你要輸出,小數,對嗎? – murasing
我只是想在一個整數的ArrayList不是雙重的ArrayList – timNorth
讀值,但我還是想輸出雙 – timNorth