A B C D F
B F F C C A
D C E F
Sample Output
2.00
1.83
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));
}
}
}
}