JAVA编程,输入五个学生的3个基础信息(姓名,学号,java课程的成绩),
package com.sinosoft.know;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Scanner;
/**
* 百度知道——统计学生分数
* 这里每个类均返回本类对象,方便调用方法
* @author cherry
*
*/
public class CalScores {
/* 学生姓名 */
private String stuName;
/* 学生学号 */
private String stuIdno;
/* 学生成绩 */
private float stuScore;
/* 前面5个存储学生姓名;後面5个存储学生学号 */
private String[] stuInfoArray = new String[10];
/* 存储学生成绩 */
private float[] stuScoreArray = new float[5];
private float allScore = 0.0f;//学生的总成绩
private float allAveg = 0.0f;//学生的平均成绩
/**
* 输入数据函数
*/
public CalScores inputData() {
Scanner input = new Scanner(System.in);
for(int i = 0; i < 5; i++) {
System.out.print("请录入学生姓名:");
stuName = input.next();
System.out.println("请录入学生学号:");
stuIdno = input.next();
/* 注这里没有加是否为数字校验,如果在成绩处输入字符将会出异常 */
System.out.println("请录入学生java课程成绩:");
stuScore = input.nextFloat();
stuInfoArray[i] = stuName;
stuInfoArray[i+5] = stuIdno;
stuScoreArray[i] = stuScore;
}
return this;
}
/**
* 数据处理函数
*/
public CalScores dealData() {
/* 计算总成绩 */
for(float a : stuScoreArray) {
allScore += a;
}
/* 计算平均值 */
allAveg = allScore / 5;
return this;
}
/**
* 输出到文件函数
*/
public CalScores outPutData() {
try {
System.out.println("文件将被写入d盤");
BufferedWriter writer = new BufferedWriter(new FileWriter(new File("d:\\Result.txt"), false));
writer.write("姓名\t学号\t成绩\n");
for(int i = 0; i < 5; i++) {
writer.write(stuInfoArray[i]+"\t\t"+stuInfoArray[i + 5]+"\t\t"+stuScoreArray[i]+"\n");
}
writer.write("总成绩为:" + allScore);
writer.write("平均成绩为:" + allAveg);
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
return this;
}
/**
* 主程序入口
* @param args
*/
public static void main(String[] args) {
CalScores cs = new CalScores();
cs.inputData().dealData().outPutData();
}
}
package test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Student {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner in = new Scanner(System.in);
String file_name = "1.txt";
try {
PrintWriter out = new PrintWriter(
new File(file_name).getAbsoluteFile());
String name;
String id;
StringBuilder temp = new StringBuilder();
int score;
int total_score = 0;
float average_score = 0f;
ArrayList<Student> data = new ArrayList<Student>();
int number;
System.out.println("please input the number of student");
number = in.nextInt();
try
{
for (int i = 0; i < number; i++)
{
System.out.println("please input "+ i+1 +"student's name");
name = in.next();
temp.append(name + '\t');
System.out.println("please input "+ i+1 +"student's id");
id = in.next();
temp.append(id + '\t');
System.out.println("please input "+ i+1 +"student's score");
score = in.nextInt();
temp.append(score);
out.println(temp.toString());
total_score += score;
}
out.println( "Total Score is:" + total_score);
out.println("Average Score is:" + total_score * 1.0 / number);
}finally
{
out.close();
}
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
throw new RuntimeException(e);
}
}
}
控制台输入,写到TXT文件里,写死
好了,楼下已经给出了代码答案,如果觉得不是想要的,再来找我吧!