java基礎編程題

來源:互聯網
上載者:User

標籤:++   ack   write   buffered   基礎   writer   double   com   void   

package com.test;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStream;
import java.util.Scanner;

public class Demo {
public static void main(String[] args) {
/*有五個學生,每個學生有3門課的成績,從鍵盤輸入以上資料(包括學生號,姓名,三門課成績),計算
*出平均成績,況原有的資料和計算出的平均分數存放在磁碟檔案"stud"中。*/

Scanner in=new Scanner(System.in);
int n=5;
String [] num=new String[n];//學號
String [] name=new String[n];//姓名
double [][] score=new double[n][3];//成績
double[]avg=new double[n];//平均分
for (int i = 0; i < n; i++) {
System.out.println("請輸入第"+(i+1)+"個學生的學號:");
num[i]=in.next();
System.out.println("請輸入第"+(i+1)+"個學生的姓名:");
name[i]=in.next();
for (int j = 0; j < 3; j++) {
System.out.println("請輸入第"+(j+1)+"門成績");
score[i][j]=in.nextDouble();
}
}
for (int i = 0; i < n; i++) {//求平均分
for (int j = 0; j < 3; j++) {
avg[i] +=score[i][j];
}
avg[i]=avg[i]/3;
}
try {
FileWriter fw=new FileWriter("c:/stud.txt");//寫入檔案
BufferedWriter bf =new BufferedWriter(fw);//緩衝流
for (int i = 0; i < n; i++) {
bf.write("學號:"+num[i]+" ");//可以寫入字串string類型的
bf.write("姓名:"+name[i]+" ");
bf.write("成績:{");
for (int j = 0;j < 3; j++) {
bf.write(score[i][j]+" ");
}
bf.write("} ");
bf.write("平均成績是"+avg[i]);
bf.newLine();//換行
}
bf.close();//釋放資源
} catch (Exception e) {
e.printStackTrace();
}


}
}

java基礎編程題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.