问题描述
我在我的 GUI 中添加了一个函数,它可以永久保存。 之前运行良好。
public void setBalance(String username,double balance){
Node current = headNode;
while(current!=null)
{
if(current.username.equalsIgnoreCase(username))
{
current.setBalance(balance);
}else{
current=current.getNext();
}
}
}
这是我写的函数,它只是卡在那里。
private void D_EnteractionPerformed(java.awt.event.ActionEvent evt) {
// Todo add your handling code here:
String username = accountUsername.getText();
String password = accountPassword.getText();
boolean flag = false;
S_Balance=l.findBalance(username,password);
Deposit_text.setVisible(true);
num1 = S_Balance + Double.parseDouble(D_deposit_text.getText()) ;
Deposit_text.setEditable(false);
Deposit_text.setText("PrevIoUs Balance:"+Double.toString(S_Balance)+"\nNew Balance:"+Double.toString(num1));
S_Balance = S_Balance + Double.parseDouble(D_deposit_text.getText());
l.setBalance(username,S_Balance);
this.N_Deposit++;
D_deposit_text.setText("");
if(S_Balance < 25)
{
Status.setVisible(true);
Active_Status.setVisible(false);
S_TextField.setText("Your Balance is less than $25");
SmileyFace.setVisible(false);
Smiley1.setVisible(true);
}else{
Status.setVisible(false);
Active_Status.setVisible(true);
S_WithDraw.setEnabled(true);
S_TextField.setText("You are good to go!!!");
SmileyFace.setVisible(true);
Smiley1.setVisible(false);
W_Enter.setEnabled(true);
}
}
这就是它被调用的地方。
解决方法
你被卡住了,因为当你找到用户名时,你没有打破 while 循环(所以它会永远运行)。您应该执行以下操作:
public void setBalance(String username,double balance){
Node current = headNode;
while(current!=null)
{
if(current.username.equalsIgnoreCase(username))
{
current.setBalance(balance);
break; // this will break the loop
}else{
current=current.getNext();
}
}
}