public class Main {
public static void main(String[] args) {
    java.util.ArrayList<StudentDataArray> info = new java.util.ArrayList<StudentDataArray>();
    int amountOfPeople = KeyboardInput.promptForInt("Enter how many people you plan to enter into the list.");
    for(int a=0;a<amountOfPeople;a++) {
        try {
        double[] grade = new double[5];
        grade[0]=KeyboardInput.promptForDouble("Enter the First grade");
        grade[1]=KeyboardInput.promptForDouble("Enter the Second grade");
        grade[2]=KeyboardInput.promptForDouble("Enter the Third grade");
        grade[3]=KeyboardInput.promptForDouble("Enter the Fourth grade");
        grade[4]=KeyboardInput.promptForDouble("Enter the Fifth grade");
            info.add(new StudentDataArray(KeyboardInput.promptForString("Enter the First Name"),KeyboardInput.promptForString("Enter the Last Name"),grade,KeyboardInput.promptForChar("Enter the Final Grade")));
        }catch(IllegalArgumentException e){
            System.out.println("Hello World :)");
        }
    }
    for(int b=0;b<amountOfPeople;b++) {
        System.out.printf("%-10s %-10s %-3f %-3s",info.get(b).getFN(),info.get(b).getLN(),info.get(b).getTS(),info.get(b).getFG());
    }
}
}

public class StudentDataArray {
private String firstName;
private String lastName;
private double[] testScore;
private char finalGrade;
public StudentDataArray(String FN, String LN, double[] TS, char FG) {
    firstName = FN;
    lastName = LN;
    testScore = TS;
    finalGrade = FG;
}
public void setFN(String FN) {firstName = FN;};
public void setLN(String LN) {lastName = LN;}
public void setTS(double[] TS) {testScore = TS;}
public void setFG(char FG) {finalGrade = FG;}
public String getFN() {return firstName;};
public String getLN() {return lastName;}
public String getTS() {
    for(int a=0;a<testScore.length;a++) {
        return testScore[a]+"   ";
    }
    return null;
}
public char getFG() {return finalGrade;}
}


大家好,这是我要看的代码。我想做的是在代码中将多个double返回一个位。它看起来像是...

伊桑·迈克尔(Ethan Michael)10 20 30 40 50 A

全部显示在一条语句中
[10 20 30 40 50]

由于我正在使用double [],因此我不确定该如何处理。

谢谢你们!

最佳答案

很多答案,但没有一个使用StringBuilder,建议您使用代替循环连接。它不会在每次迭代中创建一个新的String对象,因此在资源上要容易得多。因此:

public String getTS() {
    StringBuilder sb = new StringBuilder();
    for(int a=0;a<testScore.length;a++) {
        sb.append(testScore[a]+" ");
    }
    return sb.toString();
}

09-11 20:32