-1

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

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

错误在

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

6 回答 6

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"); 
}
于 2013-08-12T10:48:11.653 回答
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 语句并在我上面提供的代码之前声明一个全新的选项变量。这将不起作用,因为选项 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 语句将被打印(无论输入密码是否正确)。

因此,我建议您将该检查放在单独的 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");
}
于 2013-08-12T10:48:41.500 回答
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");
                     }
        }
}
于 2013-08-12T10:48:22.900 回答
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()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 和 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");
}
于 2013-08-12T10:49:07.593 回答
0

在 if-else 情况下,您不能有两个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");
    }
于 2013-08-12T10:48:15.263 回答
0

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

你有:

if { }
else { }
else { }

但你可能想要:

if { } 
else if { }
else { }
于 2013-08-12T10:48:29.213 回答