输出错误的嵌套开关大小写错误?

nested switch case error with wrong output?

我的程序输出出现一些问题。我确定错误是合乎逻辑的,但我无法修复它。错误应该在这附近

 String plusorminus ="+-"; 

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

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

/*This is my utility scanner,
 * I created char grade to get the user input.
 */
java.util.Scanner input=new java.util.Scanner(System.in);
String userInputString = input.nextLine();
char grade = userInputString.charAt(0);

我不知道如何修复它。目前,如果我将 A+ 插入程序,它会给我 A- 的结果。这是我的完整代码。

public class SwitchCase {

 public static void main(String[] args) {

System.out.println("Please enter your grade (ex.A+) to get the mark range");

/*This block of codes converts from string to
 * char and it gets the plus or minus sign
 */
String plusorminus ="+-"; 

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

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

/*This is my utility scanner,
 * I created char grade to get the user input.
 */
java.util.Scanner input=new java.util.Scanner(System.in);
String userInputString = input.nextLine();
char grade = userInputString.charAt(0);

/*This set of code contains the nested switch statements
 * that i will use to output the correct mark range
 * to the user. It also contains a try statement to find runtime
 * errors in the program.
 */
try{
switch(grade)
{
    case 'A':
        switch(modifier)
        {
            case '+': System.out.println("Your grade is 90-99.99%"); break;
            case '-': System.out.println("Your grade is 80-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;
        }
}
catch (java.util.InputMismatchException e) { //if the above error is met, message will be sent to the user
               System.out.println("Please enter a valid grade!");
         }
input.close(); //ends the user input
    }
}

之后

char grade = userInputString.charAt(0);

添加

char modifier = ' ';
if( userInputString.length() > 1 ){
    modifier = userInputString.charAt(1);
}

并删除处理 plusorminus 的代码。

您还应该添加一些代码以避免在读取输入行后出现问题。

userInputString = userInputString.trim(); // maybe user hits space?
if( ! userInputString.matches( "^[A-F][-+]?$" ) ){
    // error message...
}

不知道有没有E,还有F+或者F-??我们年级不同。也许正则表达式应该是

"^[A-E][-+]?|F$"

您可能正在插入

A+

但您只消耗了 A

String userInputString = input.nextLine();
char grade = userInputString.charAt(0);

modifier 已确定地在此处分配

String plusorminus ="+-"; 

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

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

plusorminus 的长度将始终为 2,这意味着 modifier 将始终为 plusorminus.charAt(1),即。 -。您想要将输入字符串的第二个字符分配为 modifier.