2013-06-22 63 views
-4

存款的其他部分不起作用,如果更新余额的部分为no,则存在相同的情况。如果其他部分不起作用

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

} 
+0

编辑你的代码。你复制你的班级代码.. – Masudul

+0

简短的,自包含的,正确的(可编译的),例子 – nachokk

+0

为什么你认为这将有助于重复你的问题两次? – SimonC

回答

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")) 
... 
0

如果 '如果' 块执行sc.next()那么 '其他' 块不能执行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); 
      }