剑桥大学java期末考试题一份

发表于:2007-06-22来源:作者:点击数: 标签:
Java Programming. Write a simple Java program. This program is to record marks for a maximum of 10 people. Your program is to prompt the user for a person’s name and the mark that they got. After this it should ask if the user w ant s to

   


  Java Programming.

Write a simple Java program. This program is to record marks for a maximum of 10 people. Your program is to prompt the user for a person’s name and the mark that they got. After this it should ask if the user wants to enter another mark and if they do ask for and store another. It should continue to do this till the user answers no to entering another mark.

Once this happens it should print out all the names and marks. It should also specify the min and max marks and who achieved them. It should also print out the average mark.

Finish in1 hour and half.

翻译:

编辑一个简单的程序,本程序用于记录最多10个人的成绩。要求列出每个人的名子和成绩,之后系统提示是否继续纪录:Continue Yes/No. Yes:填写下一个人的名子和成绩, No: 列出已出入的全部名子和成绩。

当输入十人之后,程序结束,列出已出入十人的名子与成绩,最大值,最小值,平均值。

限时:90分钟

本人程序:
//程序名称: MainClass.java
import java.util.*;
import java.io.*;

class ManageCenter{
private static List list = new ArrayList();
public void addStudent(Student student){
List.add(student);
}
public static int getListSize(){
return list.size();
}
public void printScore(){
System.out.println("==================================");
for(int i=0;i<list.size();i++){
System.out.println((Student)list.get(i));
System.out.println("------------------------------");
}
System.out.println("==================================");
}
public int computeMaxScore(){
int []scoreCopy = sort(getScoresList());
return scoreCopy[sort(getScoresList()).length-1];
}
public int computeMinScore(){
int []scoreCopy = sort(getScoresList());
return scoreCopy[0];
}
public int computeAverSco(){
int sumScore = 0;
int []scoreCopy = sort(getScoresList());
for(int j=0;j<scoreCopy.length;j++){
sumScore+=scoreCopy[j];
}
return sumScore/scoreCopy.length;
}
private static int[] sort(int []score){
for(int i=score.length;i>1;i--){
for(int j=0;j<score.length-1;j++){
if(score[j+1]<score[j]){
int temp = score[j+1];
score[j+1] = score[j];
score[j] = temp;
}
}
}
return score;
}
private static int [] getScoresList(){
int []score = new int[list.size()];
for(int i=0;i<list.size();i++){
Student student = (Student)list.get(i);
score [i] =student.getScore();
}
return score;
}
public void printMaxScore(){
System.out.println("最高的成绩为: "+computeMaxScore());
}
public void printMinScore(){
System.out.println("最低的成绩为: "+computeMinScore());
}
public void printAverSco(){
System.out.println("平均成绩为: "+computeAverSco());
}
}
class Student{
private String name;
private int score;
public Student(){}
public Student(String name,int score){
this.name = name;
this.score = score;
}
public void setScore(int score){
this.score = score;
}
public int getScore(){
return this.score;
}
public String toString(){
return "Name: "+this.name+" Score: "+this.score;
}
}

public class MainClass{
public static void main(String args[])throws IOException{
ManageCenter manageCenter = new ManageCenter();
System.out.println("===================================");
System.out.println("请输入学生的姓名和成绩: 格式如下所示:");
System.out.println("姓名,成绩(注意:限定格式,测试输入姓名为英文如:CoffeeCat,85)");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<10;i++){
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
String s = in.readLine();
int ind = s.indexOf(´,´);
System.out.println(ind);
Student student = new Student(s.substring(0,ind),new Integer(s.substring(ind+1)).intValue());
manageCenter.addStudent(student);
//manageCenter.printScore();
showMessage();
String str;
if((str=read.readLine())=="no"&&manageCenter.getListSize()<10){
manageCenter.printScore();
System.exit(0);
}
}
manageCenter.printScore();
manageCenter.printMaxScore();
manageCenter.printMinScore();
manageCenter.printAverSco();
}
private static void showMessage(){
System.out.println("是否继续输入记录?(answer yes/no)");
}
}

原文转自:http://www.ltesting.net