将用户输入添加到嵌套的 switch cases

Adding user input to nested switch cases

我希望用户输入他们的成绩(例如 A+),然后我会给他们结果。我需要帮助将用户输入添加到控制台,因为我真的很困惑如何在嵌套情况下添加它。

我的代码:

public class SwitchCase {

public static void main(String[] args) {

    String plusorminus ="+-";

    char mark = plusorminus.charAt(0);
    char modifier = 0;

    if(plusorminus.length() > 1)
    {
        modifier = plusorminus.charAt(1);
    }

    java.util.Scanner input=new java.util.Scanner(System.in);

    switch(mark)
    {
        case 'A':
            switch(modifier)
            {
                case '+': System.out.println("Your grade is 90-99.99%"); break;
                case '-': System.out.println("Your grade is 90-84.99%"); break;
                default: System.out.println("Your grade is 85-89.99%"); break;
            }
            break;
        case 'B':
            switch(modifier)
            {
               case'+': System.out.println("Your grade is 77.00 - 79.99%"); break;
               case'-': System.out.println("Your grade is 70.00 - 72.99%"); break;
               default: System.out.println("Your grade is 73.00 - 76.99%"); break;
            }
            break;   
        case 'C':
            switch(modifier)
             {
               case'+': System.out.println("Your grade range is 67.00 - 69.99%"); break;
               case'-': System.out.println("Your grade range is 60.00 - 62.99%"); break;
               default: System.out.println("Your grade range is 63.00 - 66.99%"); break;
             }
             break;
        case 'D':
             switch(modifier)
             {
               case'+': System.out.println("Your grade range is 55.00 - 59.99%"); break;
               case'-': System.out.println("-"); break;
               default: System.out.println("Your grade range is 50.00 - 54.99%"); break;
             }
             break;
        case 'F':
             switch(modifier)
             {
               default: System.out.println("Your grade range is 0.00-49.99%"); break;
              }    
             break;
        }
        input.close();
    }
}

input.nextLine() 将 return 一个可以分配给变量的 String。您在 switch 语句中使用 char,因此您需要获取 String 的第一个字母才能允许此操作。

java.util.Scanner input = new java.util.Scanner(System.in);
String userInputString = input.nextLine();
char mark = userInputString.charAt(0);

另一个解决方案,而不是 steven35,是这样做的(这样,你可以从你已经做过的事情中整合)

public class SwitchCase {

public static void main(String[] args) {
    Scanner scanIn = new Scanner(System.in);

    System.out.println("Input a mark");
    String line =  scanIn.nextLine();


    if (line.length()>2){
        System.out.println("ex: +a / -a / a");
        return;
    }

    char[] cA = line.substring(1, 2).toUpperCase().toCharArray();

    char[] cB = line.substring(0, 1).toUpperCase().toCharArray();

    char mark = cA[0];
    char modifier = cB[0];

    System.out.println(" INPUT: "+modifier+mark);

    switch(mark)
    {
    case 'A':
        switch(modifier)
        {
        case '+': System.out.println("Your grade is 90-99.99%"); break;
        case '-': System.out.println("Your grade is 90-84.99%"); break;
        default: System.out.println("Your grade is 85-89.99%"); break;
        }
        break;
    case 'B':
        switch(modifier)
        {
        case'+': System.out.println("Your grade is 77.00 - 79.99%"); break;
        case'-': System.out.println("Your grade is 70.00 - 72.99%"); break;
        default: System.out.println("Your grade is 73.00 - 76.99%"); break;
        }
        break;   
    case 'C':
        switch(modifier)
        {
        case'+': System.out.println("Your grade range is 67.00 - 69.99%"); break;
        case'-': System.out.println("Your grade range is 60.00 - 62.99%"); break;
        default: System.out.println("Your grade range is 63.00 - 66.99%"); break;
        }
        break;
    case 'D':
        switch(modifier)
        {
        case'+': System.out.println("Your grade range is 55.00 - 59.99%"); break;
        case'-': System.out.println("-"); break;
        default: System.out.println("Your grade range is 50.00 - 54.99%"); break;
        }
        break;
    case 'F':
        switch(modifier)
        {
        default: System.out.println("Your grade range is 0.00-49.99%"); break;
        }    
        break;
    }
    //input.close();
}

}