gpt4 book ai didi

java - 继续循环直到良好的输入?

转载 作者:行者123 更新时间:2023-12-01 06:15:00 24 4
gpt4 key购买 nike

我正在尝试制作一个程序,用户需要一个帐户才能访问它的其他部分。我希望这样设置,以便如果用户的 2 个确认密码不匹配,他们必须重新输入信息。此外,如果用户将任何内容留空,他们必须重新输入所有信息。我怎样才能做到这一点,以便它继续循环,直到用户输入正确的信息?

if (e.getSource() == okButton) {
if(!passString.equals(passStringConfirm) || userName.equals(null) || passString.equals(null) || passStringConfirm.equals(null)){
enterUsername.setText("");
enterPassword.setText("");
enterConfirmPassword.setText("");
}
}

这是我迄今为止所拥有的,并且仅适用于一次迭代。我尝试了 do while,它会不断打印出我试图在 JOptionPane 中打印的警告消息。

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.Border;

public class CreateAccount extends JFrame implements ActionListener {

JLabel username = new JLabel("Enter your username");
JTextField enterUsername = new JTextField(null, 15);
JLabel password = new JLabel("Enter your password");
JPasswordField enterPassword = new JPasswordField(null, 15);
JLabel passwordConfirm = new JLabel("Confirm your password.");
JPasswordField enterConfirmPassword = new JPasswordField(null, 15);
JButton okButton = new JButton("OK");

String userName;
double initialDeposit;

public CreateAccount() {

add(username);
add(enterUsername);
add(password);
add(enterPassword);
add(passwordConfirm);
add(enterConfirmPassword);
add(okButton);

okButton.addActionListener(this);

setTitle("New Bank Account Creation");
setVisible(true);
setLocationRelativeTo(null);
setSize(270, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());

}

@Override
public void actionPerformed(ActionEvent e) {

char[] pass = enterPassword.getPassword();
String passString = new String(pass);
char[] passConfirm = enterConfirmPassword.getPassword();
String passStringConfirm = new String(passConfirm);

userName = enterUsername.getText();

if (e.getSource() == okButton) {
if(userName == null || userName.isEmpty() || passString == null || passString.isEmpty() || !passString.equals(passStringConfirm)) {
enterUsername.setText("");
enterPassword.setText("");
enterConfirmPassword.setText("");
Border redLine = BorderFactory.createLineBorder(Color.red);
enterUsername.setBorder(redLine);
enterPassword.setBorder(redLine);
enterConfirmPassword.setBorder(redLine);
repaint();
}
}
super.dispose();

int response = 0;
String firstDesposit = JOptionPane.showInputDialog("Welcome " + userName + ". Enter your initial deposit.");
initialDeposit = Double.parseDouble(firstDesposit);
if (response == JOptionPane.OK_OPTION) {
new Menu();
}
}
}

最佳答案

您的 if 测试不正确。如果您的任何Stringnull,您将得到一个NullPointerException。我想你想要

if (userName == null || userName.isEmpty() || passString == null
|| passString.isEmpty()
|| !passString.equals(passStringConfirm)) {
enterUsername.setText("");
enterPassword.setText("");
enterConfirmPassword.setText("");
}

然后,您的 UI 代码应该在允许用户继续操作之前检查这些内容是否为空。最后,在上面的代码中,我相信您可以使用 setBorder() 为这些字段提供红色边框。

if (userName == null || userName.isEmpty() || passString == null
|| passString.isEmpty()
|| !passString.equals(passStringConfirm)) {
Border redLine = BorderFactory.createLineBorder(Color.red);
enterUsername.setText("");
enterPassword.setText("");
enterConfirmPassword.setText("");
enterUsername.setBorder(redLine);
enterPassword.setBorder(redLine);
enterConfirmPassword.setBorder(redLine);
}

编辑

基于您提供的代码,但您需要在其他代码中使用它!

if(userName == null || userName.isEmpty() || passString == null
|| passString.isEmpty() || !passString.equals(passStringConfirm)) {
enterUsername.setText("");
enterPassword.setText("");
enterConfirmPassword.setText("");
Border redLine = BorderFactory.createLineBorder(Color.red);
enterUsername.setBorder(redLine);
enterPassword.setBorder(redLine);
enterConfirmPassword.setBorder(redLine);
repaint();
} else { // <-- add this
super.dispose();
int response = 0;
String firstDesposit = JOptionPane.showInputDialog(
"Welcome " + userName + ". Enter your initial deposit.");
initialDeposit = Double.parseDouble(firstDesposit);
if (response == JOptionPane.OK_OPTION) {
new Menu();
}
}

关于java - 继续循环直到良好的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26919678/

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