字符串/案例编程错误

时间:2017-06-30 05:55:56

标签: java string if-statement case

当我把一封未列为主要字母的信件时,我能够获得无效专业,但如果我把t2,我只会获得无效专业而不是(无效专业,大二)。有人可以检测到代码并告诉我哪里出错了。

    // 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"); 
    }
}
}

1 个答案:

答案 0 :(得分:1)

卸下:

if (major == 'B' || major == 'I' || major == 'C')
{

 else{
    System.out.printf("Invalid input.%n"); 
}

您的'if语句'阻止您获得所需的输出。