5. ข้อความ

The Camtasia Studio video content presented here requires JavaScript to be enabled and the latest version of the Macromedia Flash Player. If you are you using a browser with JavaScript disabled please enable it now. Otherwise, please update your version of the free Flash Player by downloading here.

 
รหัสที่ 5-14  โปรแกรมสร้างแฟ้มเก็บผลการตัดเกรดของนักเรียน
import java.util.Scanner;
import java.io.*;
// โปรแกรมสร้างแฟ้มเก็บผลการตัดเกรดของนักเรียน 
public class GradeReport {
  public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(new File("c:/java101/scores.txt"));
    PrintStream out = new PrintStream(
                          new File("c:/java101/grades.txt"));
    while (in.hasNext()) {
      String line = in.nextLine();
      line = line.trim();
      int j = line.indexOf(" ", 0);
      if (j > 0) {
        String id = line.substring(0, j);
        String s = line.substring(j, line.length());
        double score = Double.parseDouble(s);
        String grade;
        if (score >= 80) grade = "A";
        else if (score >= 70) grade = "B";
        else if (score >= 60) grade = "C";
        else if (score >= 50) grade = "D";
        else grade = "F";
        out.println(id + "\t " + grade);              
      }
    }
    out.close();
  }
}
©2009 S.Prasitjutrakul