2015-01-04 222 views
0
System.out.println("Do you want to deposite or withdraw? (D/W)"); 
    String answer; 
    answer = input.nextLine(); 

    if("D" === answer)// Thing need to help 
    { 
    System.out.println("Enter deposite ammount: "); 
    depositeAmmount = input.nextDouble(); 
    System.out.printf("adding %.2f to account1\n\n", depositeAmmount); 
    acc1.Credit(depositeAmmount); 
    System.out.format("ballance of account1 is: %.2f\n", acc1.getBallance()); 
    System.out.format("ballance of account2 is: %.2f\n\n", acc2.getBallance()); 
    } 
    else if(){ 
    System.out.println("Enter withdraw ammount: "); 
    withdrawAmmount = input.nextDouble(); 
    System.out.printf("differing %.2f to account1\n\n", depositeAmmount); 
    acc1.Withdraw(withdrawAmmount); 

    System.out.format("ballance of account1 is: %.2f\n", acc1.getBallance()); 
    System.out.format("ballance of account2 is: %.2f\n", acc2.getBallance()); 
    } 
    else{ 
     System.out.println("The answer you input is invalid! Please input again!\n"); 
     answer = input.nextLine(); 
    } 

正如你所看到的,我想使用if来创建提取和存款操作,如何将这个答案分配给字符D或W来执行此代码。你可以向我展示怎么做?非常感谢!如何使用if(answer =='D')?

+1

使用'answer.equals( “d”)'或'回答。等于(“W”)来检查相等性。 – 2015-01-04 10:42:10

+0

查看[this](http://stackoverflow.com/questions/4505357/what-is-the-difference-between-operator-and-equals-with-hashcode)==和等号的区别 – SMA 2015-01-04 10:42:57

回答

0

您可以使用类似:

if(answer.equalsIgnoreCase("D")) { 
    // your code 
} 

为了弥补不同情况下的字母输入d或d等