gpt4 book ai didi

Java ATM 项目逻辑错误

转载 作者:行者123 更新时间:2023-12-02 06:36:17 26 4
gpt4 key购买 nike

这是我的源代码:

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;
}
}

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

最佳答案

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

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

现在它仅适用于第一个组合,因为您也在其他部分打破了循环。因此,它从不检查第二个值之后的值

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;
}

关于Java ATM 项目逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19615653/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com