我的代码有问题,它在输入中跳过行检查和节省

时间:2013-11-26 16:22:12

标签: java jdeveloper

我的代码会跳过我必须输入检查和储蓄值的行。我应该创建这个:“编写一个程序来模拟银行交易。有两个银行账户:支票和储蓄。•首先要求银行账户的初始余额;拒绝负余额。•然后要求交易;选项是存款,取款和转账。 •然后询问帐户;选项是检查和节省。 •然后询问金额;拒绝透支账户的交易。 •最后打印两个帐户的余额。

这是我的代码:

import java.util.Scanner;

public class BankTransaction {

static Scanner  keyb = new Scanner(System.in);
public static void main(String[] args) {
    double checkings=0, savings=0,depAmount,withdrawAmount,transferAmount;
    int n=0, oper, oper2; 

    System.out.print("Please specify what type of account you have. Type 1 for checkings, type 2 for savings.");
    //checkings= keyb.nextDouble(); 

    if (n==1) {    //case 1 of the transaction

    System.out.print("Please enter the intitial value of your checkings account");

    checkings= keyb.nextDouble(); 

    while (checkings<=0){
    System.out.print("Your initial account should be greater than zero. Please type a positive number.");
    checkings=keyb.nextDouble();}
    }//end of choice 1
    else
        if (n==2){
    System.out.print("Please enter the intitial value of your savings account");
    savings= keyb.nextDouble();}//end of choice 2

    while (savings<=0){
    System.out.print("Please type a positive number.");
    savings=keyb.nextDouble();}

    System.out.print("Please type 1 for  operation within your checking account or 2 for operations within your savings account.");
            oper=keyb.nextInt();
    while((oper < 1) || (oper > 2)) {
           System.out.print("Invalid input.");
           System.out.print ("Type 1 for operation within deposit account, type 2 for operation within savings account");
           oper= keyb.nextInt();}




    if (oper==1){

        System.out.print("Please specify what type of transaction you want to perform: 1 for deposit, 2 for transaction, 3 for transfer"); 
        n  = keyb.nextInt(); 

        while((n < 1) || (n > 3)) {//input validation for the type of transaction the user wants to perform
           System.out.println("Invalid input.");
           System.out.print ("Type 1 for deposit, 2 for transaction, 3 for transfer");
           n = keyb.nextInt();
        }

        if (n==1) {    //case 1 of the transaction
            System.out.print("Please type the amount you want to deposit");
            depAmount= keyb.nextDouble();//initial value inserted for the depositing amount

            while (depAmount<=0){//positive input validation of the depositing amount
                System.out.print("Invalid input. ");
                System.out.print("Please type an amount greater than 0.");
                depAmount = keyb.nextDouble();
            }// end of while of depAmount

            checkings= checkings + depAmount;
            System.out.println("Your new deposit amount = " + checkings  );

        }// end of if deposit

        else
            if (n==2){
                System.out.print("Please type the amount you want to withdraw ");
                withdrawAmount=keyb.nextDouble();

                while (withdrawAmount<=0 && withdrawAmount>checkings){
                    System.out.print("The amount can not be bigger than the inital balance or smaller or equal with zero.");
                    System.out.print("Please type the amount you want to withdraw");
                    withdrawAmount=keyb.nextDouble();
                }

                checkings= checkings-withdrawAmount;
                System.out.println("Your new deposit amount = " + checkings  );
            }//end of if

                else {
                    System.out.print ("Please type the amount you want to transfer");
                    transferAmount=keyb.nextDouble();
                    while (transferAmount <0 && transferAmount > checkings); {
                        System.out.println("The amount you typed is invalid.The transfer amount can't be bigger than the initial checkings acount, nor smaller than zero");
                        System.out.print("Please type the amount you want to transfer.");
                        transferAmount=keyb.nextDouble();
                    }// end of input validation 

                    checkings= checkings + transferAmount; 
                    System.out.println("Your new deposit amount = " + checkings  );
                }// end of nested else
        }//end of if oper 1

    //*******************************************************************************************************

    else {
        if (oper==2){

            System.out.print("Please specify what type of transaction you want to perform: 1 for deposit, 2 for transaction, 3 for transfer"); 
            n  = keyb.nextInt(); 

            while((n < 1) || (n > 3)) {//input validation for the type of transaction the user wants to perform
               System.out.println("Invalid input.");
               System.out.print ("Type 1 for deposit, 2 for transaction, 3 for transfer");
               n = keyb.nextInt();
            }

            if (n==1) {    //case 1 of the transaction
                System.out.print("Please type the amount you want to deposit");
                depAmount= keyb.nextDouble();//initial value inserted for the depositing amount

                while (depAmount<=0){//positive input validation of the depositing amount
                    System.out.print("Invalid input. ");
                    System.out.print("Please type an amount greater than 0.");
                    depAmount = keyb.nextDouble();
                }// end of while of depAmount

                savings= savings + depAmount;
                System.out.println("Your new deposit amount = " + checkings  );

            }// end of if deposit

            else
                if (n==2){
                    System.out.print("Please type the amount you want to withdraw ");
                    withdrawAmount=keyb.nextDouble();

                    while (withdrawAmount<=0 && withdrawAmount>checkings){
                        System.out.print("The amount can not be bigger than the inital balance or smaller or equal with zero.");
                        System.out.print("Please type the amount you want to withdraw");
                        withdrawAmount=keyb.nextDouble();
                    }

                    savings= savings-withdrawAmount;
                    System.out.println("Your new deposit amount = " + checkings  );
                }//end of if

                    else {
                        System.out.print ("Please type the amount you want to transfer");
                        transferAmount=keyb.nextDouble();
                        while (transferAmount <0 && transferAmount > savings); {
                            System.out.println("The amount you typed is invalid.The transfer amount can't be bigger than the initial savings acount, nor smaller than zero");
                            System.out.print("Please type the amount you want to transfer.");
                            transferAmount=keyb.nextDouble();
                        }// end of input validation 

                        savings= savings + transferAmount; 
                        System.out.println("Your new deposit amount = " + checkings  );
                    }// end of nested else
            }//end of if oper 1
    } // end of else oper 2     

}//end of main 
}// end of class

1 个答案:

答案 0 :(得分:0)

永远不会输入您的while循环,因为n永远不会是1。

如果您使用n来表示用户的选择,那么至少应该让用户输入n。您可以在代码中稍后输入oper的方式执行此操作。

int n=0, oper, oper2; 

System.out.print("Please specify what type of account you have. Type 1 for checkings, type 2 for savings.");
//checkings= keyb.nextDouble(); 

if (n==1) { 
    ... do stuff