如何獲取此表以便在表中打印?我寫的方式我無法弄清楚如何在最後打印所有結果。這是我有:從循環創建表(java)
import java.util.Scanner;
public class FFLAverage{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Input the number of students");
int numofstu = input.nextInt();
double totalchange = 0;
for (int count = numofstu; count != 0; count--){
if (count == numofstu)
System.out.println("Last Name \t\t StartWeight \t\t End Weight \t\t Weight Change");
System.out.print("Enter student name: ");
String name = input.next();
System.out.print("Input start weight: ");
double start = input.nextDouble();
System.out.print("Enter end weight: ");
double end = input.nextDouble();
double change = (start - end);
totalchange += change;
System.out.println(name + "\t\t" + start + "\t\t\t" + end + "\t\t\t" + change);
}
double avg = totalchange/numofstu;
System.out.println("Average weight change: " + avg);
}
}
這是輸出應該是什麼樣子(控制檯)
Last Name Start Weight End Weight Weight Change
Noble 222.5 202.4 -23.1
Tyler 189.5 194.4 +4.9
Pond 345.1 190.0 -155.1
Average weight change: --57.33
得到什麼打印的表?你認爲什麼是結果 - 也許提供一些樣本輸出? – John3136
如果你想要所有的名字等,你需要將它們存儲到一個集合中,而不僅僅是單個變量(這將在下一次通過循環時丟失) – John3136