【问题标题】:Java ATM project logical error(s)Java ATM 项目逻辑错误
【发布时间】:2013-10-27 07:25:52
【问题描述】:

这是我的源代码:

import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;
public class ATM {

String names []= {"Nikhil", "Furrqaan", "Raj", "Saron", "Abishiek", "Aditya"};
static int pin_no;static int card_no;
static int card[]={1806978,1244668,5769124,7658301,6571354,5155499};
static int pin[]={4444, 3333, 2222, 1111, 6666, 7988};
int balance []={17867, 23345, 24670, 37532, 43637, 37356};
int account[]={219075286,156482798,456482748,465295772,665793758,565738957};
int f,ch,z;
static int r = 0;
static int opt=0;
Scanner s=new Scanner(System.in);
public static void main(String args []) {
    int pincode=0;      
    int repeat=0;   
    ATM k=new ATM();
    do{
    k.info();
    k.login();
    for(int choice=0;choice<=5;choice++)
    if(card_no==card[choice] && pin_no==pin[choice]){
    k.menu(choice);
    break;
    }else{
    System.out.println("Wrong card no. or pincode");
    break;
    }
    k.End(repeat);
    }while(k.End(repeat)==0);

}

   void info(){
   Calendar calender = Calendar.getInstance();
   SimpleDateFormat dateformatter = new SimpleDateFormat(" dd EEEEEEEEE',' yyyy ");
   SimpleDateFormat timeformatter = new SimpleDateFormat(" hh:mm:ss a");
   System.out.println("");
   System.out.println("                  ****************************************************************       ");
   System.out.println("                  ***                         HSBC's  ATM                     ****       ");
   System.out.println("                  ****************************************************************       ");
   System.out.println("                  Date - "+ dateformatter.format(calender.getTime()));
   System.out.println("                  Time - "+timeformatter.format(calender.getTime()));
   System.out.println("                  ****************************************************************       ");
   System.out.println("");
   }

  void login (){
   System.out.println("                  >> Enter card number - ");
   card_no=s.nextInt();
   System.out.println("                  >> Enter pin number - ");
   pin_no= s.nextInt();
  }

void menu (int choice){
    System.out.println("                  Welcome "+names[choice]);
    System.out.println("                  Your Account no: is " + account[choice]);
    System.out.println("                  ###############################################        ");
    System.out.println("                  >>> [1]. Cash Withdrawal ");
    System.out.println("");
    System.out.println("                  >>> [2]. Fast Cash ");
    System.out.println("");
    System.out.println("                  >>> [3]. Cash Deposit ");

     opt=s.nextInt();

    switch (opt){
    case 1:
        Calendar calender = Calendar.getInstance();
        SimpleDateFormat dateformatter = new SimpleDateFormat(" dd'/'MM ");
        SimpleDateFormat timeformatter = new SimpleDateFormat(" HH:mm ");
        ATM k=new ATM();
        int ammount;
        System.out.println("                  ==========================  ");
        System.out.println("                  CASH WITHDRAWAL ");
        System.out.println("                  ==========================  ");
        System.out.print("                    Enter amount to withdraw - Rs.");
        ammount=s.nextInt();
        System.out.println("                  ::::::::::::::::::::::::::::::::::::::::::::::");
        System.out.println("                  Please wait while we process your request...");

    if (ammount>balance[z]){
        System.out.println("                  You do not have sufficient balance in your account.");
        k.menu(choice);
        for(z=1;z<=1000000000;z++)
        System.out.print("\f");
    }else if (ammount>25000){
        System.out.println("                  The daily limit for cash withdrawal is Rs.25000 .");
        k.menu(choice);
    }else{
        balance[z]=balance[z]-ammount;

        System.out.println("");
        System.out.println("                  You have withdrawn Rs."+ammount+" from your account.");
        System.out.println("                  Please collect the cash.");
        System.out.println("                  Current Balance - Rs."+balance[z]);
        System.out.println("                  ::::::::::::::::::::::::::::::::::::::::::::::");
       }
        break; 
    case 2:
        int fast[]={100,500,1000,5000};
        System.out.println("                  ==========================  ");
        System.out.println("                  FAST CASH WITHDRAWAL ");
        System.out.println("                  ==========================  ");
        System.out.println("                  Please select a desired amount.");
        System.out.println("                  (1) Rs. 100 ");
        System.out.println("                  (2) Rs. 500 ");
        System.out.println("                  (3) Rs. 1000 ");
        System.out.println("                  (4) Rs. 5000 ");
        System.out.print("                     Your choice ? ");
        ch=s.nextInt();
        if (fast[ch-1]>balance[z])
        {
        System.out.println("                  You do not have sufficient balance in your account.");
        }else{
            switch(ch)
            {
            case 1:
            balance[z]=balance[z]-fast[ch-1];
            break;
            case 2:
            balance[z]=balance[z]-fast[ch-1];
            break;
            case 3:
            balance[z]=balance[z]-fast[ch-1];
            break;
            case 4:
            balance[z]=balance[z]-fast[ch-1];
            break;
            }
        }
        System.out.println("");
        System.out.println("                  You have withdrawn Rs. "+fast[ch-1]+" from your account.");
        System.out.println("                  Please collect the cash.");
        System.out.println("                  Current Balance - Rs."+balance[z]);
        System.out.println("                  ::::::::::::::::::::::::::::::::::::::::::::::");
        break;
    case 3:
        System.out.println("                  ==========================  ");
        System.out.println("                  CASH DEPOSIT ");
        System.out.println("                  ==========================  ");
        System.out.print("                  Enter amount to deposit - Rs.");
        int deposite =s.nextInt();
        System.out.println("                  ::::::::::::::::::::::::::::::::::::::::::::::");
        System.out.println("                  Please wait while we process your request...");
        System.out.println("");
        System.out.println("                  You have deposited Rs."+deposite+" to your account.");
        System.out.println("                  The amount will be credited shortly.");
        System.out.println("                  Current Balance - Rs."+balance[z]+" + Rs."+deposite);
        System.out.println("                  ::::::::::::::::::::::::::::::::::::::::::::::");
        break;
    case 4:
        System.out.println("                  ==========================  ");
        System.out.println("                  BALANCE ENQUIRY ");
        System.out.println("                  ==========================  ");
        System.out.println("                  Account Number - "+card[z]);
        System.out.println("                  Available Balance - Rs."+balance[z]);
        System.out.println("                  ::::::::::::::::::::::::::::::::::::::::::::::");
        System.out.println("                  Please collect the reciept...");
  }
}
 int End(int repeat){
    ATM k=new ATM();
    System.out.println();
    System.out.println("                      What would you like to do next?           ");
    System.out.println("                      [1]. Enter main menu.                     ");
    System.out.println("                      [2]. Exit.                                ");
    int option=s.nextInt();
    switch (option){
    case 1:
        repeat=0;
        break;
    case 2:
        repeat=1;
        break;
    default:
        System.out.println("Please check input.");
    }
    return repeat;
   }
 }  

当唯一有效的卡号和密码组合是第一个时。其他一切 打印错误消息,即“错误的密码或卡号”。程序的最后一个功能(无效重复)打印“你接下来想做什么?”分两次。任何帮助表示赞赏。

【问题讨论】:

  • 第一个?您的意思是唯一有效的卡/密码组合是您的阵列中的第一个,还是您的意思是如果用户第一次输入任何有效的组合,然后它就无效?
  • 输入法错误,需要键盘模拟器。
  • 我的意思是只有第一个卡/针组合有效@djna

标签: java function loops logic switch-statement


【解决方案1】:

让我们从第二个问题开始。您正在调用 End 方法两次。所以难怪它会打印两次。

k.End(repeat);
}while(k.End(repeat)==0);

现在它仅适用于第一个组合,因为您也打破了 else 部分的循环。因此,它永远不会检查第二个值

for(int choice=0;choice<=5;choice++)
if(card_no==card[choice] && pin_no==pin[choice]){
k.menu(choice);
break;
}else{
System.out.println("Wrong card no. or pincode");
break;
}

【讨论】:

    【解决方案2】:

    注释上面一行代码k.End(repeat); while 条件试一试。它只会显示一次。

    //k.End(repeat);
        } while (k.End(repeat) == 0);
    

    【讨论】:

      【解决方案3】:

      我没有立即看出这里出了什么问题,但有两个更普遍的观察结果:

      1)。不要写完所有这些然后开始测试。分小块工作。测试 login() 函数,它是否可靠地返回了用户输入的两个数字?一旦你有信心测试这两个数字对你的数组的验证,等等。

      2)。使用调试器。只需单步执行您的代码并检查一些变量,就可以快速向您展示发生了什么。

      【讨论】:

      • 虽然这些都是很好的建议,但对这里的用户没有帮助。问题出在 main 方法本身
      • @GV 我不同意。 1)。无论代码在哪里,调试肯定会有所帮助吗? 2)。通过对做什么工作有信心,你可以分析必须打破的东西。分析方法是解决更大问题的关键。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多