gpt4 book ai didi

java - 由于If语句无法登录

转载 作者:行者123 更新时间:2023-12-02 04:07:11 24 4
gpt4 key购买 nike

我想登录系统。当使用 ID 和帐号创建客户时,我尝试输入它们,并希望如果 ID 和帐户已连接 - 对话框将打开。

当我创建客户 ID 时,在构造函数中我将其转换为字符串:

Customer(int id, String pps, String name, String surname, String birthday, String password){

this.id = nextId.incrementAndGet();
Integer.toString(this.id);

当我在另一个类中创建帐户时,但对于客户,我在构造函数中执行了相同的操作(我将帐号转换为字符串):

public CustomerAccount(int accountNumber, double balance){

Random n = new Random();
this.accountNumber = 100000 + n.nextInt(900000);
Integer.toString(this.accountNumber);

在我的驱动程序类中,我有按钮,然后,当我按下按钮时,对话框应该打开。

depositW.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
JFrame frame = new JFrame(" ");
boolean found1 = false;
boolean found = false;


String a = JOptionPane.showInputDialog("Please enter the ID number: ");
for(Customer aCustomer: customers){
System.out.println("ID: " + a + " = " + aCustomer.getId());
if(a.equals(aCustomer.getId())){
BankInterface.setCurrentCustomer(aCustomer);
found1 = true;
}else{
JOptionPane.showMessageDialog(frame, "There is no customer with this ID number", "Please try again", JOptionPane.WARNING_MESSAGE);
}

if(found1){
String b = JOptionPane.showInputDialog("Please enter the account number: ");
for(CustomerAccount cu: BankInterface.getCurrentCustomer().getAccounts()){
System.out.println("Account: " + a + " = " + cu.getAccountNumber());
if(b.equals(cu.getAccountNumber())){

BankInterface.setCurrentAccount(cu);
found = true;
}
}
}

if(found && found1){
new WithdrawDepositDialog(BankInterface.this, customers);
}else{
JOptionPane.showMessageDialog(frame, "The username or password is not correct", "Please try again", JOptionPane.WARNING_MESSAGE);
}
}
}
});

当我测试它时,在我的控制台窗口中出现以下内容:所以它在运行时找不到 id。

ID: 1 = 1
ID: 1 = 2

我现在确定了,问题出在哪里。我猜我的 if 语句中有什么问题。这个问题可能的解决方案是什么?

<小时/>

更新的代码:

String a = JOptionPane.showInputDialog("Please enter the ID number: ");
for (Customer aCustomer: customers) {
System.out.println("ID: " + a + " = " + aCustomer.getId());
if (a.equals(aCustomer.getId())) {
BankInterface.setCurrentCustomer(aCustomer);
found1 = true;
break;
}
}
if (!found1) {
JOptionPane.showMessageDialog(frame, "There is no customer with this ID number", "Please try again", JOptionPane.WARNING_MESSAGE);
}

最佳答案

代码运行得很好。

您对所有客户的循环:

for(Customer aCustomer: customers){

不在此终止:

if(a.equals(aCustomer.getId())){
BankInterface.setCurrentCustomer(aCustomer);
found1 = true;
}

因为found1 = true不是循环条件。您在这里缺少的是在该行之后的一个简单的 break;

关于java - 由于If语句无法登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34162137/

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