GPA
http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=1§ionid=2&problemid=19
这个程序学到了怎样用 BufferedReader 读一行数据。。以前都用的Scanner。。。。
1 //author:pz 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.text.DecimalFormat; 7 8 public class Main { 9 public static void main(String[] args) throws IOException { 10 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 11 String s = in.readLine(); 12 while (s != null) { 13 System.out.println(calGPA(s)); 14 s = in.readLine(); 15 } 16 } 17 18 private static String calGPA(String s) { 19 DecimalFormat df = new DecimalFormat("0.00"); 20 double gpa = 0.0; 21 for (int i = 0; i <= s.length() / 2; i++) { 22 switch (s.charAt(2 * i)) { 23 case 'A': 24 gpa += 4; 25 break; 26 case 'B': 27 gpa += 3; 28 break; 29 case 'C': 30 gpa += 2; 31 break; 32 case 'D': 33 ++gpa; 34 break; 35 case 'F': 36 break; 37 default: 38 return "Unknown letter grade in input"; 39 } 40 } 41 gpa /= (s.length() / 2 + 1); 42 return df.format(gpa); 43 } 44 }