-4

預金のelse ifセクションが機能しておらず、残高を更新するためのnoの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 に答える