JAVA编程题求高手帮忙!
实现以下功能:1.从键盘输入姓名、学号、成绩并且保存到文件"Results.dat"中。2.重复输入直到输入空字符串为止(直接回车)。3.打印出学生成绩中最高分最低分和平...
实现以下功能:1.从键盘输入姓名、学号、成绩并且保存到文件"Results.dat"中。2.重复输入直到输入空字符串为止(直接回车)。3.打印出学生成绩中最高分最低分和平均成绩。4.打印出成绩前三名的学生成绩单。谢谢!
展开
3个回答
展开全部
功能完成,健壮性不强,楼主可以在修改下
参考代码
================================================
package test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class ScoreTable {
private ArrayList<Node> table=new ArrayList<Node>();
private float max=Float.MIN_VALUE,min=Float.MAX_VALUE,ave=0;
private Node[] top3=new Node[3];
public void read() throws IOException{
String s=null;
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
do{
s=in.readLine();
if(s!=null && s.length()!=0){
System.out.println("["+s+"]"+s.length());
String[] tmp=s.split(" ");
System.out.println("len:"+tmp.length);
table.add(new Node(tmp[0],tmp[1],Float.parseFloat(tmp[2])));
}
}while(s.length()!=0);
}
public void saveToFile() throws IOException{
FileWriter fw = new FileWriter(System.getProperty("user.dir")+"\\Results.dat");
BufferedWriter bw=new BufferedWriter(fw);
for(Node n : table){
bw.write(n.getStuNum()+" "+n.getName()+" "+n.getScore());
bw.newLine();
}
bw.close();
}
public void analyzer(){
float sum=0;
for(Node n : table){
float tmp=n.getScore();
if(tmp<min)
min=tmp;
if(tmp>max)
max=tmp;
sum+=tmp;
if(top3[0]==null || tmp>top3[0].getScore())
top3[0]=n;
else if(top3[1]==null || tmp>top3[1].getScore())
top3[1]=n;
else if(top3[2]==null || tmp>top3[2].getScore())
top3[2]=n;
}
ave=sum/table.size();
}
public void output(){
System.out.println("max: "+max);
System.out.println("min: "+min);
System.out.println("ave: "+ave);
System.out.println("top3 students:");
for(int i=0;i<top3.length;i++)
System.out.println(top3[i]);
}
public static void main(String[] args) throws IOException {
ScoreTable st=new ScoreTable();
st.read();
st.saveToFile();
st.analyzer();
st.output();
}
}
class Node {
private String name,stuNum;
private Float score;
Node(String n, String sN, Float s){
name=n;
stuNum=sN;
score=s;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStuNum() {
return stuNum;
}
public void setStuNum(String stuNum) {
this.stuNum = stuNum;
}
public Float getScore() {
return score;
}
public void setScore(Float score) {
this.score = score;
}
public String toString(){
return "stuNum: "+stuNum+" , name: "+name+" , score: "+score;
}
}
================================================
参考代码
================================================
package test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class ScoreTable {
private ArrayList<Node> table=new ArrayList<Node>();
private float max=Float.MIN_VALUE,min=Float.MAX_VALUE,ave=0;
private Node[] top3=new Node[3];
public void read() throws IOException{
String s=null;
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
do{
s=in.readLine();
if(s!=null && s.length()!=0){
System.out.println("["+s+"]"+s.length());
String[] tmp=s.split(" ");
System.out.println("len:"+tmp.length);
table.add(new Node(tmp[0],tmp[1],Float.parseFloat(tmp[2])));
}
}while(s.length()!=0);
}
public void saveToFile() throws IOException{
FileWriter fw = new FileWriter(System.getProperty("user.dir")+"\\Results.dat");
BufferedWriter bw=new BufferedWriter(fw);
for(Node n : table){
bw.write(n.getStuNum()+" "+n.getName()+" "+n.getScore());
bw.newLine();
}
bw.close();
}
public void analyzer(){
float sum=0;
for(Node n : table){
float tmp=n.getScore();
if(tmp<min)
min=tmp;
if(tmp>max)
max=tmp;
sum+=tmp;
if(top3[0]==null || tmp>top3[0].getScore())
top3[0]=n;
else if(top3[1]==null || tmp>top3[1].getScore())
top3[1]=n;
else if(top3[2]==null || tmp>top3[2].getScore())
top3[2]=n;
}
ave=sum/table.size();
}
public void output(){
System.out.println("max: "+max);
System.out.println("min: "+min);
System.out.println("ave: "+ave);
System.out.println("top3 students:");
for(int i=0;i<top3.length;i++)
System.out.println(top3[i]);
}
public static void main(String[] args) throws IOException {
ScoreTable st=new ScoreTable();
st.read();
st.saveToFile();
st.analyzer();
st.output();
}
}
class Node {
private String name,stuNum;
private Float score;
Node(String n, String sN, Float s){
name=n;
stuNum=sN;
score=s;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStuNum() {
return stuNum;
}
public void setStuNum(String stuNum) {
this.stuNum = stuNum;
}
public Float getScore() {
return score;
}
public void setScore(Float score) {
this.score = score;
}
public String toString(){
return "stuNum: "+stuNum+" , name: "+name+" , score: "+score;
}
}
================================================
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询