有五个学生,他们有三门课程(语文,数学,英语),现在从键盘输入这些内容,计算这些学生的成绩总和和平均值.把学生的成绩总和和平均值输出到磁盘的stud.
package com.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Scanner;
public class Shuchu {
public static void main(String[] args) throws IOException {
String str;
File file = new File("D:\\stud.txt");
OutputStream os = new FileOutputStream(file,false);
for(int i=1;i<=5;i++){
System.out.println("请输入第" + i + "个学生的成绩:");
int score = 0;
int sum = 0;
int avg = 0;
for(int j=1;j<=3;j++){
System.out.println("请输入第"+i+ "学生的第"+j+ "门课程成绩:");
Scanner sc = new Scanner(System.in);
score = sc.nextInt();
sum += score;
str = "第" + i + "个学生的第" + j + "门课程成绩是" + score + ", ";
os.write(str.getBytes());
}
avg = sum/3;
str="第" + i + "个学生的总成绩是:" + sum + "平均成绩是"+ avg + ", ";
System.out.println("第" + i + "个学生的总成绩是:" + sum + "平均成绩是"+ avg);
os.write(str.getBytes());
os.write('\r');
os.write('\n');
}
}
}
package com.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Scanner;
public class Shuchu {
public static void main(String[] args) throws IOException {
String str;
File file = new File("D:\\stud.txt");
OutputStream os = new FileOutputStream(file,false);
for(int i=1;i<=5;i++){
System.out.println("请输入第" + i + "个学生的成绩:");
int score = 0;
int sum = 0;
int avg = 0;
for(int j=1;j<=3;j++){
System.out.println("请输入第"+i+ "学生的第"+j+ "门课程成绩:");
Scanner sc = new Scanner(System.in);
score = sc.nextInt();
sum += score;
str = "第" + i + "个学生的第" + j + "门课程成绩是" + score + ", ";
os.write(str.getBytes());
}
avg = sum/3;
str="第" + i + "个学生的总成绩是:" + sum + "平均成绩是"+ avg + ", ";
System.out.println("第" + i + "个学生的总成绩是:" + sum + "平均成绩是"+ avg);
os.write(str.getBytes());
os.write('\r');
os.write('\n');
}
}
}