Unknown letter grade in input
import java.text.DecimalFormat;
import java.util.Scanner;
public class PracticeVI {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#.00");
String input;
Double sum,value;
Boolean flag;
while(sc.hasNextLine()){
sum = 0.0;value=0.0;
flag = false;
input = sc.nextLine();
String[] chs = input.split(" ");
for(int i=0; i<chs.length; i++){
if("A".equals(chs[i]))
sum = sum + 4;
else if("B".equals(chs[i]))
sum = sum + 3;
else if("C".equals(chs[i]))
sum = sum + 2;
else if("D".equals(chs[i]))
sum = sum + 1;
else if("F".equals(chs[i]))
sum = sum + 0;
else if(" ".equals(chs[i]))
continue;
else{
flag = true;
break;
}
}
if(flag)
System.out.println("Unknown letter grade in input");
else{
value = sum / chs.length;
System.out.println(df.format(value));
}
}
}
}
原文地址:http://blog.csdn.net/lian_li/article/details/45191691