2 回答

TA貢獻1852條經驗 獲得超1個贊
請將您的代碼修改為:
public class Main {
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter(new File("F://test.txt"));
Scanner sc = new Scanner(System.in);
int students = 2;
double[] doubleAVG = new double[students];
char[] charGRADE = new char[students];
double avg = 0.0;
char gradeLetter;
String FirstName = "";
String LastName = "";
for (int count = 0; count < students; count++) {
System.out.println("Enter average :");
avg = sc.nextDouble();
System.out.println("Enter grade :");
gradeLetter = sc.next().charAt(0);
System.out.println("Enter First Name :");
FirstName = sc.next();
System.out.println("Enter Last Name :");
LastName = sc.next();
doubleAVG[count] = avg;
charGRADE[count] = gradeLetter;
fw.write("This is the grade of: " + FirstName + " " + LastName + " ");
fw.write(String.valueOf(doubleAVG[count]) + " ");
fw.write(charGRADE[count] + System.lineSeparator());
}
fw.close();
sc.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
控制臺輸入:
Enter average :
70.45
Enter grade :
B
Enter First Name :
John
Enter Last Name :
Johnson
Enter average :
90.47
Enter grade :
A
Enter First Name :
Michael
Enter Last Name :
Jordan
在test.txt中:
This is the grade of: John Johnson 70.45 B
This is the grade of: Michael Jordan 90.47 A

TA貢獻1995條經驗 獲得超2個贊
您是否知道 java Serialize 方法(學),它創建了您在類文件中擁有的所有數據結構及其對象的 xml 文件,使其中的嵌套結構看起來像特定時間點的對象,就像原始 xml 一樣?這可能是由于 java 在 5 種確切的本機數據類型上中繼,而對象只是這些數據類型的集合??纯瓷厦娴脑u論以及 Stackoverflow 答案的鏈接,我只是向您解釋了一些有關機制的信息。
添加回答
舉報