0

好吧抱歉,如果这是一个非常愚蠢的问题(我对 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 );


    }        
}
4

1 回答 1

0

第一个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();
于 2014-10-17T23:04:43.893 回答