/* * T01n06.java -- Based on the given letter grade, this program will display * an encouraging message to the user. This demonstrates how to indent * a deeply-nested set of IF statements. Doing it the normal way makes the * text too wide to be conveniently viewed or printed. * * This is the first program we've seen that uses chars. You can read * chars, write them, and compare them. If you want to use a char as a * constant literal, put the symbol in single quotes, like this: 'K' * Don't use the left quote (`) for this, though. * * Questions: Would this program accept 'b' as a legal letter grade? * Should it? If so, how would you change the program to * accept the lower-case versions of the legal letter grades? * * Note: To get a single character from the keyboard, the Scanner class * forces us to read the entire line and pick out the character. This * means reading a String, and we haven't quite got to the String class * yet. So, just accept this for now, and we'll explain it later. */ import java.util.*; public class T01n06 { public static void main (String [] args) { char grade; // A student's letter grade (A, B, C, D, or E) Scanner keyboard = new Scanner (System.in); System.out.print("Enter your letter grade and I'll print an " + "encouraging message : "); grade = keyboard.nextLine().charAt(0); // See comment above. if (grade == 'A') { System.out.println("You don't need encouragement!"); } else if (grade == 'B') { System.out.println("You did very well!"); } else if (grade == 'C') { System.out.println("You're pleasingly average!"); } else if (grade == 'D') { System.out.println("At least you didn't fail!"); } else if (grade == 'E') { System.out.println("Try again next semester!"); } else { System.out.println("That's not a legal letter grade!"); } } }