2013-11-26 2 views
0

Мой код пропускает строки, где я должен вводить значения проверки и сохранения. Я должен был создать это: «Напишите программу для имитации банковской транзакции. Существует два банковских счета: проверка и сбережения. • Сначала попросите начальные балансы банковских счетов, отклоните отрицательные балансы. • Затем попросите сделку; Варианты - депозит, снятие и перевод. • Затем запросите учетную запись, параметры проверки и сбережения. • Затем попросите сумму, отклоните транзакции, которые перегружают учетную запись. • В конце распечатывайте остатки обеих учетных записей.У меня проблема с моим кодом, он пропускает строки на входе для проверок и сбережений

Вот мой код:

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

Советы - напишите свою программу для выполнения одной функции за раз - прямо сейчас вы понятия не имеете, что делает и не работает. – KevinDTimm

ответ

0

ваше время цикла никогда не будет введен, потому что 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 
Смежные вопросы