- 題目:有五個學(xué)生,每個學(xué)生有3門課的成績,從鍵盤輸入以上數(shù)據(jù)(包括學(xué)生號,姓名,三門課成績),計算出平均成績,把原有的數(shù)據(jù)和計算出的平均分?jǐn)?shù)存放在磁盤文件 "stud "中。
1 public class _50AvgandGrade {
2
3 public static void main(String[] args) {
4 avgandGrade();
5 }
6
7 private static void avgandGrade() {
8 Scanner ss = new Scanner(System.in);
9 String[][] a = new String[5][6];
10 for (int i = 1; i < 6; i++) {
11 System.out.print("請輸入第" + i + "個學(xué)生的學(xué)號:");
12 a[i - 1][0] = ss.nextLine();
13 System.out.print("請輸入第" + i + "個學(xué)生的姓名:");
14 a[i - 1][1] = ss.nextLine();
15 for (int j = 1; j < 4; j++) {
16 System.out.print("請輸入該學(xué)生的第" + j + "個成績:");
17 a[i - 1][j + 1] = ss.nextLine();
18 }
19 System.out.println("\n");
20 }
21 // 以下計算平均分
22 float avg;
23 int sum;
24 for (int i = 0; i < 5; i++) {
25 sum = 0;
26 for (int j = 2; j < 5; j++) {
27 sum = sum + Integer.parseInt(a[i][j]);
28 }
29 avg = (float) sum / 3;
30 a[i][5] = String.valueOf(avg);
31 }
32 // 以下寫磁盤文件
33 String s1;
34 try {
35 File f = new File("C:\\stud");
36 if (f.exists()) {
37 System.out.println("文件存在");
38 } else {
39 System.out.println("文件不存在,正在創(chuàng)建文件");
40 f.createNewFile();// 不存在則創(chuàng)建
41 }
42 BufferedWriter output = new BufferedWriter(new FileWriter(f));
43 for (int i = 0; i < 5; i++) {
44 for (int j = 0; j < 6; j++) {
45 s1 = a[i][j] + "\r\n";
46 output.write(s1);
47 }
48 }
49 output.close();
50 System.out.println("數(shù)據(jù)已寫入c盤文件stud中!");
51 } catch (Exception e) {
52 e.printStackTrace();
53 }
54
55 }
56
57 }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。