Java 标记“else”上的语法错误,删除它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18185023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:06:00  来源:igfitidea点击:

Syntax error on token "else", delete this

javaif-statementsyntax

提问by user2674614

I keep on getting this error and I tried mixing it around. But then when I choose the option, it does the option, but then said "you did not enter 1, 2 or 3".

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

This is the the full code. How to fix it?

这是完整的代码。如何解决?

The error is at

错误是在

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

采纳答案by Andrew Martin

The problem is in the code you provided on your paste bin.

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

You use two else statements, so Java complains as it doesn't know which to go to after the initial if statement.

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

You need to enter in another conditional statement using else if, then else. For example:

您需要使用 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");   
}

You also have another major problem with your code. You declare the variable option and set it in an if statement, so it only has scope within that if statement. You then come out of the if statement and declare a brand new option variable before the code I provided above. This will not function, as the option integer has no value.

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

Instead, you need to declare your initial option int outside of your if statement, like so:

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

int number;
int password = 7123;
int amount = 4000;
int option;

if (number == password) {
    etc...
    option = userInput.nextInt();
}

Furthermore, you come out of the if statement checking the entered number against the stored password to take input on withdrawing cash etc. This is no good. It means that after the if statement checking number against password is finished, it will automatically proceed to the next block of code.

此外,您从 if 语句中检查输入的数字与存储的密码以获取提取现金等的输入。这是不好的。这意味着在 if 语句检查数字对密码完成后,它将自动进行下一段代码。

As the scanner object hasn't been created, the first three if statements will come back false and your else statement will be printed (regardless of whether the input password was correct).

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

Therefore, I would advise you to put that check in a separare else statement and use a while loop to confirm a correct selection ahs been entered. For example:

因此,我建议您将该检查放在单独的 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");
}

回答by MS_SP

This is an incorrect syntax:

这是一个不正确的语法:

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

Just do it this way:

只需这样做:

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

回答by Jayamohan

You cannot have TWOelse in a if-else case.

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

Your nested If statement is not proper. Use it as below

您嵌套的 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");
    }

回答by No Idea For Name

your second elsestatement does not close no ifstatement.

您的第二个else语句不会关闭 noif语句。

Also, the optionvariable is not in the right scope:

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

try this

尝试这个

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

回答by Robin van Breukelen

The problem is that you the 'else' statement twice in your if-else construct.

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

You have:

你有:

if { }
else { }
else { }

But you probably want:

但你可能想要:

if { } 
else if { }
else { }

回答by Ravi Thapliyal

You can't have two elseblocks added to an if

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

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

Either drop one or club the two println()s into one elseblock

要么丢一个,要么把两个都println()塞进一个else街区

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

Take a look at how to use the if-then and if-then-elsestatements.

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

Alternatively, I suggest you to make use of the Switchstatements for better clarity of code.

或者,我建议您使用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");
}