Sunday, October 8, 2017

if(another example)

Challenge: Write a program that reads a letter grade and displays “Excellent” if the letter is an A, “Good” if it is a B, “You can do better!” if it’s a C, “I’m afraid that’s not good” if it’s a D or F, and “Input Error!” in any other case. Implement a solution using only IF statements.

import java.util.Scanner;

public class lab5_1
{
public static void main(String []args)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter you grade:");
char answer=in.nextLine().charAt(0);
   
if (answer=='A' || answer== 'a')
    System.out.println("Excellent");




else if (answer=='B' || answer== 'b')
    System.out.println("Good");

else if(answer=='C' || answer== 'c')
    System.out.println("You can do better");
   
else if (answer=='D' || answer== 'd'|| answer== 'F'|| answer== 'f')
    System.out.println("I'm afraid that's not good");
   
else
    System.out.println("Input Error!"); 
}
}

No comments:

Post a Comment