看看吧
import java.util.*;
import java.text.*;
public class SchoolRecordDemo1 {
String pattern = "^-+?0-9*\\.?0-9+$";
Vector students = new Vector();
DecimalFormat formatter = new DecimalFormat("#.##");
public SchoolRecordDemo1() {
Scanner scanner = new Scanner(System.in);
out("Please type Student Number 0 - Exit : ");
String line = scanner.nextLine();
while (!line.equals("0")) {
while (!line.matches(pattern)) {
while(!line.matches(pattern)) {
out("It's not a number! Try again. ");
out("Please type Student Number 0 - Exit : ");
line = scanner.nextLine();
}}
Student student = new Student(line);
student.getEachScore(scanner);
students.addElement(student);
out("Please type Student Number 0 - Exit : ");
line = scanner.nextLine();
}}
private void out(String s) {
if(s!=null)
System.out.print(s);}
public void listAverage() {
double sum=0, avg=0;
for(int i=0 ; i
Student student = students.elementAt(i);
sum += student.getAverage();}
avg = sum/((double)students.size());
out("Number of Students : "+students.size()+", Total Average: "+formatter.format(avg)+"\n");}
public void listAll() {
for(int i=0 ; i
Student student = students.elementAt(i);
out(student.toString());
out("\n");}}
class Student {
String studentNo;
String ITEMS = {"Chinese","English","Mathematics"};
double scores = new doubleITEMS.length;
double sum=0,avg=0;
public Student(String line) {studentNo = line;}
public void getEachScore(Scanner scanner) {
String line = "";
for (int i=0 ; i
out("Please type the score of "+ITEMSi+" : ");
line = scanner.nextLine();
while(!line.matches(pattern)){
out("It's not a number! Try again. Please type the score of "+ITEMSi+" : ");
line = scanner.nextLine();}
scoresi = Double.parseDouble(line);
sum+=scoresi;
}
avg = (sum/(double)scores.length);}
public double getAverage() {return avg;}
◆◆
评论读取中....
请登录后再发表评论!
◆◆
修改失败,请稍后尝试