现在的位置: 首页 > 综合 > 正文

SSD3-EXAM1

2013年08月26日 ⁄ 综合 ⁄ 共 2227字 ⁄ 字号 评论关闭
import  java.io.*;
import  java.util.*;

/* DOCUMENT THIS CLASS */
public class StudentApplication  {

    /* Standard input stream */
    private static BufferedReader  stdIn =
        new  BufferedReader(new  InputStreamReader(System.in));

    /* Standard output stream */
    private static PrintWriter  stdOut =
        new  PrintWriter(System.out, true);

    /* Standard error stream */
    private static PrintWriter  stdErr =
        new  PrintWriter(System.err, true);

    /* Delimiter of student values */
    private static final String DELIM = "_";

    /* Minimum grade */
    private static final int MIN_GRADE = 0;

    /* Maximum grade */
    private static final int MAX_GRADE = 100;

    /**
     * This program reads, from the standard input, a student's grades;
     * and then displays, to the standard output, that student's average.
     *
     * @param args  not used
     * @throws IOException  if error reading from the standard input.
     */
    public static void main(String[]  args) throws IOException  {

        Student student = readStudent();

        stdOut.println("The average of the student is: "  +
            student.computeAverage());
    }

    /* DOCUMENT THIS METHOD */
    public static Student readStudent() throws IOException {

        String name = "";
        int exam1 = 0;
        int exam2 = 0;
        int exam3 = 0;

        /* PLACE YOUR CODE HERE */
       
        do{
            try{
                 stdErr.println();
                 stdErr.print("student [name_exam1_exam2_exam3]> ");
                 stdErr.flush();
                 StringTokenizer  tokenizer =
                           new  StringTokenizer(stdIn.readLine(),DELIM);
                
                 if(tokenizer.countTokens()==4){
                    name=tokenizer.nextToken();
                    exam1=Integer.parseInt(tokenizer.nextToken());
                    exam2=Integer.parseInt(tokenizer.nextToken());
                    exam3=Integer.parseInt(tokenizer.nextToken());
                   
                    if(exam1<0||exam1>100||exam2<0||exam2>100||exam3<0||exam3>100){
                         stdErr.println("Number out of range");
                    }else{
                         return new Student(name, exam1, exam2, exam3);
                    }
                       
                 }else{
                    stdErr.println("Invalid input line");
                 }
                  
            }catch(IOException e){
                 stdErr.println("Incorrect number format in the line");
            }

        }while(true);

    }
}

抱歉!评论已关闭.