令牌“else”上的语法错误,删除此项

时间:2013-08-12 10:45:08

标签: java if-statement syntax

我继续收到此错误,我尝试将其混合使用。但是当我选择该选项时,它会选择,但随后说“你没有输入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");
}

6 个答案:

答案 0 :(得分:3)

这是一种不正确的语法:

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

答案 1 :(得分:1)

您的第二个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");
                     }
        }
}

答案 2 :(得分:1)

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

你使用了两个else语句,所以Java抱怨因为它不知道在初始if语句之后要去哪个。

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

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语句中退出并在我上面提供的代码之前声明一个全新的选项变量。这将不起作用,因为选项integer没有值。

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

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

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

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

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

因此,我建议您将该检查放入separare 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 :(得分:0)

if-else案例中不能包含 TWO

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

答案 4 :(得分:0)

问题是你在if-else构造中两次使用'else'语句。

你有:

if { }
else { }
else { }

但你可能想要:

if { } 
else if { }
else { }

答案 5 :(得分:0)

您无法在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()一个或一个else块放入一个<{1}}块

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