如何从 while 循环/for 循环中捕获变量 [closed]

How to capture a variable from within a while loop/for loop [closed]

提问人:beansies 提问时间:10/15/2023 更新时间:10/15/2023 访问量:68

问:


这个问题是由一个错别字或一个无法再重现的问题引起的。虽然类似的问题可能在这里成为主题,但这个问题的解决方式不太可能帮助未来的读者。

上个月关闭。

我正在做一个小项目只是为了练习数组/循环。它基本上是一个银行页面,您可以注册并登录。我希望能够在用户注册时将他们的用户名和密码输入存储到数组列表中,然后当用户登录时 - 系统将检查用户名是否与数据库中的名称匹配(用户数组列表)或密码是否匹配(传递数组列表)。但是,当我尝试通过打印数组列表来测试它时,我发现变量没有存储在数组中。如何将用户值正确地存储到数组中,以便以后可以使用它们?希望这一切都是有道理的。谢谢。

import javax.swing.*;
import java.util.ArrayList;


public class Main {
    public static void main(String[] args) {
        ImageIcon icon = new ImageIcon("cutepig.jpg");
        String[] response1 = {"Sign up", "Log in", "Exit"};
        int options01 = JOptionPane.showOptionDialog(null, "Welcome to Beanie's Bank!", "Beanie's Bank", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, icon, response1, 0);
        ArrayList<String> users = new ArrayList<>();
        ArrayList<String> passes = new ArrayList<>();

        while (true) {
            if (options01 == 0) {
                JTextField username = new JTextField();
                users.add(username.getText());
                JTextField password = new JPasswordField();
                passes.add(password.getText());
                JTextField confpass = new JPasswordField();
                Object[] message = {
                        "Username: ", username,
                        "Password: ", password,
                        "Confirm Password: ", confpass,
                };
                int signup1 = JOptionPane.showConfirmDialog(null, message, "Sign up", JOptionPane.OK_CANCEL_OPTION);
                if (signup1 == JOptionPane.OK_OPTION) {
                    if (password.getText().equals(confpass.getText()) && !password.getText().isEmpty() && !confpass.getText().isEmpty()) {
                        JOptionPane.showMessageDialog(null, "Sign up was successful! \n Welcome to Beansie's Bank. \n Please log in to access your account!");
                        break;
                    } else if (password.getText().isEmpty() || confpass.getText().isEmpty() || username.getText().isEmpty()) {
                        JOptionPane.showMessageDialog(null, "Input cannot be empty, please enter a valid input.");
                        continue;
                    } else if (!password.getText().equals(confpass.getText())) {
                        JOptionPane.showMessageDialog(null, "Your passwords do not match! Please try again.");
                        continue;
                    }
                }
            }
        }



        while(true){
        JTextField username = new JTextField();
        JTextField password = new JPasswordField();
        Object[] message = {
                "Username: ", username,
                "Password: ", password
        };

        int option = JOptionPane.showConfirmDialog(null, message, "login", JOptionPane.OK_CANCEL_OPTION);

            if (username.getText().equals(users) && password.getText().equals(passes)) {
                JOptionPane.showMessageDialog(null, "Login successful, welcome back " + username + ".");
                break;
            } else if (!username.getText().equals(users) && !password.getText().equals(passes)){
                JOptionPane.showMessageDialog(null,"Login failed, please try again.");
                continue;
            }
            else{
            System.out.println("Login canceled");
            continue;
            }
    }
}}
Java 数组 if-statement while 循环

评论


答:

1赞 Marcin Orlowski 10/15/2023 #1

在用户有机会输入其凭据之前,将 and 字段添加到 and 列表中,因此这些字段当时是空的。您应该在检查 .usernamepassworduserspassesOK_OPTION

while (true) {
    if (options01 == 0) {
        JTextField username = new JTextField();
        JTextField password = new JPasswordField();
        JTextField confpass = new JPasswordField();
        Object[] message = {
                "Username: ", username,
                "Password: ", password,
                "Confirm Password: ", confpass,
        };
        int signup1 = JOptionPane.showConfirmDialog(null, message, "Sign up", JOptionPane.OK_CANCEL_OPTION);
        if (signup1 == JOptionPane.OK_OPTION) {
            if (password.getText().equals(confpass.getText()) && !password.getText().isEmpty() && !confpass.getText().isEmpty()) {
                users.add(username.getText());
                passes.add(password.getText());
                JOptionPane.showMessageDialog(null, "Sign up was successful! \n Welcome to Beansie's Bank. \n Please log in to access your account!");
                break;
            } //... rest of your code
        }
    }
}
0赞 marcinj 10/15/2023 #2

代码中存在两个问题。

首先是这里:

JTextField username = new JTextField();
users.add(username.getText());
JTextField password = new JPasswordField();
passes.add(password.getText());

您正在将用户名/密码添加到数组列表,然后才真正知道。此时,用户尚未指定其用户名/密码。您应该在用户确认其输入后移动行和 。修复后的代码应如下所示:users.add(username.getText());passes.add(password.getText());

int signup1 = JOptionPane.showConfirmDialog(null, message, "Sign up", JOptionPane.OK_CANCEL_OPTION);
  if (signup1 == JOptionPane.OK_OPTION) {
     if (password.getText().equals(confpass.getText()) && !password.getText().isEmpty() && !confpass.getText().isEmpty()) {
         users.add(username.getText()); // !!
        passes.add(password.getText()); // !!

第二个是这个代码:

if (username.getText().equals(users) && password.getText().equals(passes)) {
                      ^^^^^^^^^^^^^                       ^^^^^^^^^^^^^^^

users并声明为:passes

    ArrayList<String> users = new ArrayList<>();
    ArrayList<String> passes = new ArrayList<>();

因此,您不能与其中任何一个进行比较或(a)。这不会发出编译器错误或运行时异常,只是在这里始终返回 false。您应该遍历数组列表,并将用户输入与每个元素进行比较。诸如此类:username.getText()password.getText()Stringequals

for (int i = 0; i < users.size(); i++) {
    if (username.getText().equals(users.get(i)) && password.getText().equals(passes.get(i))) {
        JOptionPane.showMessageDialog(null, "Login successful, welcome back " + username.getText() + ".");
        break;
    }
}