0

我正在尝试开一家银行,我正在做一个扫描仪选项,所以如果你按 1,你可以按 2 取款,你将加钱,按 3 离开银行。但是当我按 2 或 3 时,它什么也没做,我尝试 else if 但是我得到更多的错误,我不知道有什么区别。

------注意------ 我尝试取消嵌套(如果那是一个短语),但我在按钮附近的 else 上出现语法错误(我已经标记了位置)所以我不知道如何解决这个问题

import java.util.Scanner;




public class Bank {




    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();

        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);
        int option;
        option = userInput.nextInt();

        if (option == 1) {
        //Withdraw Money System
        System.out.println("You Have £4000");
        System.out.println("How Much Would You Like To Take Out?");
        Scanner Input = new Scanner (System.in);
        int numbere;


        numbere = userInput.nextInt();
        if (numbere < 4000) {

        int money = amount - numbere;
            System.out.println("You Have Now £" + money);
            System.out.println("Thank You For Banking At Harry's Bank!");
            System.out.println("Please Come Again!");
          }else{
                System.out.println("You Do Not Have Enough Money!"); 
          }
        }


        else if (option == 2) {
            //AddMoney System
            Scanner AddMoney = new Scanner (System.in);
            int AddMoney1;
            AddMoney1 = userInput.nextInt();
            int NewMoney = (amount + AddMoney1);
            System.out.println("How Much Would You Like To Enter?");
            System.out.println("You Now Have " + NewMoney + "!");
            System.out.println("Thank You For Using Harry's Bank!");




        }
        else if (option == 3) {
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("Goodbye!");
        }
        }else{
            System.out.println("Next Time Only Press 1,2 or 3!");
        }
         //error Here For Else "Syntax error on token "else", { expected"
        }else{
                System.out.println("Pin Declined!");

          }



            }








        }
4

3 回答 3

8

你的括号是关闭的。你有

if(option ==1)
{
     if(option == 2)
     {
         //stuff
     }

     if(option == 3)
     {
         //stuff
     }
}

将那些 ifs 移出第一个

于 2013-05-03T19:46:34.747 回答
1

因为第 3 和第 4 个 if 语句是遥不可及的。如果你不按 1,他们将永远无法到达。

于 2013-05-03T19:44:40.750 回答
0

您的 if 语句嵌套在 if for "option == 1" 中,因此您无法访问选项 2 或 3,这应该是 else if 的链

于 2013-05-03T19:50:10.600 回答