-4

存款的 else if 部分不起作用,与 else if 部分的情况相同,用于更新余额。

    public class BankAccount
    {


    static double Balance;

    public static void main(String[] args) 
    {
    Scanner sc = new Scanner(System.in);
    System.out.println("enter the account holder's name...");
    String Name = sc.next();
    System.out.println("enter his account number...");
    long AccNo = sc.nextLong();
    System.out.println("enter his type of account...");
    String AccTyp = sc.next();
    System.out.println("enter his current balance...");
    Balance = sc.nextDouble();

    System.out.println("Do you want to update your balance, press y for yes and n for                      no...");
    if(sc.next().equalsIgnoreCase("y") == true)
        {
           System.out.println("To withdraw money press w, To deposite the same, press        d");

           if(sc.next().equalsIgnoreCase("w") == true)
                { 
                    System.out.println("Enter the amount the account holder withdrawed");
                    double withdraw = sc.nextDouble();
                    double Bal = Withdraw(withdraw);
                    System.out.println("Account holder " +Name+ " with account number " +AccNo+ " and account type " +AccTyp+ " has the new balance = "+Bal);
                }
          else if(sc.next().equalsIgnoreCase("d") == true)
                {
                    System.out.println("Enter the amount the account holder deposited");
                    double deposit = sc.nextDouble();
                    double Bal = Deposit(deposit);
                    System.out.println("Account holder " +Name+ " with account number " +AccNo+ " and account type " +AccTyp+ " has the new balance = "+Bal);
                }
        else
            System.out.println("Thank you for your transaction");
        }


    else if(sc.next().equalsIgnoreCase("n") == true)
    {
        System.out.println("Thank you for your transaction");
    }
    System.out.println("Thank you for your transaction");
 }


public static double Withdraw(double a)
    {
        Balance = Balance - a;
        return Balance;
    }

public static double Deposit(double b)
    {
        Balance = Balance + b;
        return Balance;
    }

 }
4

2 回答 2

6

不要继续调用该next()方法:

if(sc.next().equalsIgnoreCase("w") == true)
...
else if(sc.next().equalsIgnoreCase("d") == true)

相反,代码应该是这样的:

String response = sc.next();

if(response.equalsIgnoreCase("w"))
...
else if(response.equalsIgnoreCase("d"))
...
于 2013-06-22T04:57:26.403 回答
0

如果 'if' 块执行sc.next() ,则 'else' 块不能执行sc.next()。将输入分配给这样的变量操作。

    String action=sc.next();
    if(action.equalsIgnoreCase("w") == true)
            { 
                System.out.println("Enter the amount the account holder withdrawed");
                double withdraw = sc.nextDouble();
                double Bal = Withdraw(withdraw);
                System.out.println("Account holder " +Name+ " with account number " +AccNo+ " and account type " +AccTyp+ " has the new balance = "+Bal);
            }
    else if(action.equalsIgnoreCase("d") == true)
            {
                System.out.println("Enter the amount the account holder deposited");
                double deposit = sc.nextDouble();
                double Bal = Deposit(deposit);
                System.out.println("Account holder " +Name+ " with account number " +AccNo+ " and account type " +AccTyp+ " has the new balance = "+Bal);
            }
于 2013-06-22T05:01:49.173 回答