jeudi 29 juin 2017

String/Case programing error

I was able to get invalid major when I put a letter that is not listed major, but if I put t2, I would only get invalid major and not (invalid major, sophomore). Can someone detect the code and tell me where I went wrong.

    // Enter two characters

    System.out.print("Enter two characters: ");
    String status = in.next();

    char major = Character.toUpperCase(status.charAt(0));
    char year = status.charAt(1);

    String courseName = "";
    String yearName = "";

    // majors

    if (major == 'B' || major == 'I' || major == 'C')
    {
        switch(major)
        {
            case 'B':
                courseName = "Biology"; break;
            case 'C':
                courseName = "Computer Science"; break;
            case 'I':
                courseName = "Information Technology"; break;

            default:System.out.println("Invaild major"); break;
        }

        // year

        switch(year)
        {
            case '1':
                yearName = "Freshman"; break;
            case '2':
                yearName = "Sophmore"; break;
            case '3':
                yearName = "Junior"; break;
            case '4':
                yearName = "Senior"; break;

            default: System.out.println("Invalid year status"); break;
        }

        System.out.printf("%s %s%n", courseName, yearName);
    }
    else{
        System.out.printf("Invalid input.%n"); 
    }
}

}

Aucun commentaire:

Enregistrer un commentaire