我如何将用户输入的答案之一设置为应用该操作的答案



好吧,如果这是一个非常愚蠢的问题(我对java真的很陌生),很抱歉,但我如何设置用户是否将节省或检查到其中一个if-else语句。例如,如果我想向支票或储蓄存款500,并且我输入了储蓄,但我如何告诉java他们选择了哪个账户,以便它可以存款或取款?

package javaapplication3;
import java.util.Scanner
public class Transfer 
{
    public static void main(String[] args)
    {
        int checking = 1000;
        int savings = 1000;
        int amount = 0;

        Scanner in = new Scanner(System.in);
        System.out.println("ballance of checking: " + checking );
        System.out.println("ballance of savings: " + savings );
        System.out.println("What is the ammount you wish to deposit, transfer, or withdraw?: " );
        amount = in.nextInt();
        if    (amount < 0 )
        {
           System.out.println("sorry you cant do that please only input positive amounts"); 
        }
        else
        {
            System.out.println(amount);
        }
        System.out.println("which account will you be useing checking or savings? : " );
        String account = in.next();
        if (account.equals("checking"))
        {
            System.out.println("ballance of checking: " + checking );
        }    
        else if (account.equals("savings"))
        {
            System.out.println("ballance of savings: " + savings );
        }
        else 
        {
        System.out.println("something went wrong"  );
        }
        System.out.println(" will you be transferring depositing or withdrawing?: " );
        String action = in.next();
        if (action.equals("transfering"))
        {
            System.out.println("transferring: " + amount);
            System.out.println("account: " + account );
        }
        else if (action.equals("depositing"))   
        {  
        System.out.println("depositing: " + amount);
        System.out.println("account: " + account );
        }      
        else if (action.equals("withdrawing"))            
        {
            System.out.println("withdrawing: " + amount );
            System.out.println("account: " + account );

        }        
        else
        {
        System.out.println("something went wrong: ");
        }
        System.out.println("ballance of checking: " + checking );
        System.out.println("ballance of savings: " + savings );

    }        
}

第一个in.nextInt();没有消耗回车。这样做:

        amount = in.nextInt();
        in.nextLine();
        if    (amount < 0 )
        {
           System.out.println("sorry you cant do that please only input positive amounts"); 
        }
        else
        {
            System.out.println(amount);
        }
        System.out.println("which account will you be useing checking or savings? : " );
        String account = in.next();

相关内容

最新更新