【问题标题】:Syntax error on token "else", delete this [closed]标记“else”的语法错误,删除这个[关闭]
【发布时间】:2013-08-12 10:45:07
【问题描述】:

我不断收到此错误,并尝试将其混合在一起。但是当我选择该选项时,它执行了该选项,但是然后说“您没有输入1、2或3”。

这是full code。如何解决?

错误在

} else {
    System.out.println("You did not enter 1, 2 or 3");
} else {
    System.out.println("The Pin You Entered Was Wrong");
}

【问题讨论】:

标签: java if-statement syntax


【解决方案1】:

这是一个错误的语法:

 }else{
        System.out.println("You did not enter 1, 2 or 3");
 }else{
        System.out.println("The Pin You Entered Was Wrong"); 

就这样做吧:

else {
    System.out.println("You did not enter 1, 2 or 3");
    System.out.println("The Pin You Entered Was Wrong"); 
}

【讨论】:

    【解决方案2】:

    问题出在您在粘贴箱中提供的代码中。

    您使用了两个 else 语句,因此 Java 会抱怨,因为它不知道在最初的 if 语句之后去哪个。

    您需要使用 else if, then else 输入另一个条件语句。例如:

    if (option == 1){
        Option_1 Optionone = new Option_1();
        Optionone.Withdraw();
    }
    etc
    
    }else if (nothing entered) {
        System.out.println("You did not enter 1, 2 or 3");
    }else{
        System.out.println("The Pin You Entered Was Wrong");   
    }
    

    您的代码还有另一个主要问题。您声明变量选项并将其设置在 if 语句中,因此它仅在该 if 语句中具有范围。然后,您退出 if 语句并在我上面提供的代码之前声明一个全新的选项变量。这将不起作用,因为选项整数没有值。

    相反,您需要在 if 语句之外声明初始选项 int,如下所示:

    int number;
    int password = 7123;
    int amount = 4000;
    int option;
    
    if (number == password) {
        etc...
        option = userInput.nextInt();
    }
    

    此外,您从 if 语句中检查输入的数字与存储的密码以获取提取现金等的输入。这是不好的。就是说if语句对密码校验数字完成后,会自动进入下一段代码。

    由于尚未创建扫描仪对象,前三个 if 语句将返回 false,并且您的 else 语句将被打印(无论输入密码是否正确)。

    因此,我建议您将该检查放在单独的 else 语句中,并使用 while 循环来确认输入了正确的选择。例如:

    System.out.println("Please Enter Your Bank Pin.");
    Scanner userInput = new Scanner (System.in);
    int number;
    int password = 7123;
    int amount = 4000;
    int option;
    
    number = userInput.nextInt();
    
    if (number == password) {
        System.out.println("Pin Accepted");
        System.out.println("You Have Now Entered Harry's Bank!");
        System.out.println("Press The Number Of The Option You Would Like.");
        System.out.println("1.Withdraw Money.");
        System.out.println("2.Put In Money");
        System.out.println("3.Exit Bank");
        Scanner Options = new Scanner (System.in);
        option = userInput.nextInt();
        while (option != 1 || option != 2 || option != 3) {
            System.out.println("You didn't enter a valid number. Try again");
            option = userInput.nextInt();
        }
        if (option == 1){
            Option_1 Optionone = new Option_1();
            Optionone.Withdraw();
        }
        else if (option == 2){
            Option_2 Optiontwo = new Option_2();
            Optiontwo.Deposit();
        }
        else if (option == 3){
            Option_3 Optionthree = new Option_3();
            Optionthree.Exit();
        }
    }
    else
        System.out.println("The Pin You Entered Was Wrong");
    }
    

    【讨论】:

      【解决方案3】:

      你的第二个else 语句没有关闭没有if 语句。

      另外,option 变量不在正确的范围内:

      试试这个

      import java.util.Scanner;
      
      public class Main {
      
              public static void main(String[] args) {
                      System.out.println("Welcome To Harry's Bank");
      
                      //Pin System
      
                      System.out.println("Please Enter Your Bank Pin.");
                      Scanner userInput = new Scanner (System.in);   
                              int number;    
                              int password = 7123;    
                              int amount = 4000;   
                              number = userInput.nextInt();
                              int option;
      
                              if (number == password) {
                                  System.out.println("Pin Accepted");
                                  System.out.println("You Have Now Entered Harry's Bank!");
                                  System.out.println("Press The Number Of The Option You Would Like.");
                                  System.out.println("1.Withdraw Money.");
                                  System.out.println("2.Put In Money");
                                  System.out.println("3.Exit Bank");
                                  Scanner Options = new Scanner (System.in);
                                  option = userInput.nextInt();
                              }else{
                                  System.out.println("The Pin You Entered Was Wrong");    
                              }
      
      
                              if (option == 1){
                                      Option_1 Optionone = new Option_1();
                                      Optionone.Withdraw();
                              }
      
                               if (option == 2){
                                      Option_2 Optiontwo = new Option_2();
                                      Optiontwo.Deposit();
                               }
      
                               if (option == 3){
                                      Option_3 Optionthree = new Option_3();
                                      Optionthree.Exit();
                               }else{
                                      System.out.println("You did not enter 1, 2 or 3");
                           }
              }
      }
      

      【讨论】:

        【解决方案4】:

        问题是您在 if-else 构造中使用了两次“else”语句。

        你有:

        if { }
        else { }
        else { }
        

        但你可能想要:

        if { } 
        else if { }
        else { }
        

        【讨论】:

          【解决方案5】:

          在 if-else 情况下,您不能有 TWO else。

          您的嵌套 If 语句不正确。如下使用它

              if (number == password) {
                  .....
                  .....
          
                  if (option == 1) {
                  }
                  else if (option == 2) {
                  }
                  else if (option == 3) {
          
                  } else {
                      System.out.println("You did not enter 1, 2 or 3");
                  }
              } else {
                  System.out.println("The Pin You Entered Was Wrong");
              }
          

          【讨论】:

            【解决方案6】:

            您不能将两个 else 块添加到 if

            } else {
                System.out.println("You did not enter 1, 2 or 3");
            } else {
                System.out.println("The Pin You Entered Was Wrong");
            }
            

            要么放弃一个,要么将两个println()s 合并为一个else

            } else {
                System.out.println("You did not enter 1, 2 or 3");
                System.out.println("The Pin You Entered Was Wrong"); 
            }
            

            看看如何使用 if-then and if-then-else 语句。

            或者,我建议您使用 Switch 语句以使代码更清晰。

            switch (option) {
                case 1:  Option_1 OptionOne = new Option_1();
                         OptionOne.Withdraw();
                         break;
            
                case 2:  Option_2 OptionTwo = new Option_2();
                         OptionTwo.Withdraw();
                         break;
            
                case 3:  Option_3 OptionThree = new Option_3();
                         OptionThree.Withdraw();
                         break;
            
                default: System.out.println("You did not enter 1, 2 or 3");
                         System.out.println("The Pin You Entered Was Wrong");
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2021-05-29
              • 1970-01-01
              • 1970-01-01
              • 2014-12-03
              • 1970-01-01
              • 1970-01-01
              • 2018-12-05
              相关资源
              最近更新 更多