dimanche 17 juin 2018

Rules not obeyed by if statement

The below code is supposed to tell the user whether his input is a vowel or consonant or digit less than 1. The program however returns "Vowel" for all capital letters including consonants and whenever a digit is entered "Consonant" is returned. Ex - if a is entered, result = vowel. If b, result = Consonant. If B, result = Vowel (should be Consonant). If 1, result = Consonant(should be Digit) . Any advice would be greatly appreciated. Thanks.

package checkVowelorConstantorNumber;
import java.util.Scanner;
public class Main {
public static void main (String [] args) {
    Scanner inp = new Scanner (System.in);
    boolean tf = false;


    while(tf == false) {
    System.out.println("Enter a character which is a - z or A - Z or 
    less than 1");
    char cha = inp.next().charAt(0);
    inp.nextLine();

    if(Character.isLetter(cha) && cha == 'a'||cha == 'e' || cha == 'i' || cha == 'o' || cha == 'u' || Character.isUpperCase(cha)) {
     System.out.println("Vowel");
    }
    else if(Character.isLetter(cha) && cha != 'a'|| cha != 'e' || cha != 'i' || cha != 'o' || cha != 'u' || Character.isUpperCase(cha)) {
     System.out.println("Consonant");
    }

    else if(Character.isDigit(cha) && cha <= 1 ) {
     System.out.println("Digit");
    }
    else  System.out.println("Invalid character");
    }
 }
}

Aucun commentaire:

Enregistrer un commentaire