循环不工作 java

Looping not Working java

我正在 Java 中创建一个登录程序。经过如此努力,我几乎完成了,但我还有最后一个问题。我希望我的程序在关闭前只有 3 次登录尝试,问题是在您输入错误的密码和用户之后它会继续循环,而无需用户再次输入用户名和密码,然后在 3 次循环后关闭。请帮助我。

package password;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

public class Password extends JFrame {


public JPasswordField pass;
public JLabel usernameL,passwordL;
public JTextField usernameTF,passwordTF;
private JButton LogInB,CancelB;
private LogInButtonHandler logHandler;
private CancelButtonHandler canHandler;


public Password (){
    usernameL=new JLabel("Username");
    passwordL=new JLabel("Password");
    usernameTF=new JTextField(20);
    passwordTF=new JTextField(20);
   pass=new JPasswordField(10);


  pass.setEchoChar ('•');

    LogInB=new JButton("Log In");
    logHandler=new LogInButtonHandler();
    LogInB.addActionListener(logHandler);

    CancelB=new JButton("Cancel");
    canHandler=new CancelButtonHandler();
    CancelB.addActionListener(canHandler);

    setTitle("Log In");
    Container p=getContentPane();
    p.setLayout(new GridLayout(3,3));

    p.add(usernameL);
    p.add(usernameTF);
    p.add(passwordL);
    p.add(pass);
    p.add(LogInB);
    p.add(CancelB);



    setSize(300,200);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

}

    private class LogInButtonHandler implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {


        String password=null;
        String user=null;
        boolean isValid;



       Scanner in= new Scanner(System.in);




        for(int i=0;i<3;i++)
        {

        try {
            if(i!=0)
            {
                JOptionPane.showMessageDialog(null,"Username/Password Inccorect Please Try Again: "+i,"Error",JOptionPane.ERROR_MESSAGE);
            }

            if(i==2)
            {
                JOptionPane.showMessageDialog(null,"This " +(++i) + "rd login will be your final attempt","Error",JOptionPane.ERROR_MESSAGE);
            }





            isValid=isValidCred(user,password);
            if(isValid)
            {
                JOptionPane.showMessageDialog(null,"Welcome "+user,"Succesful",JOptionPane.INFORMATION_MESSAGE );
                break;
            }

            if(i==3)
            {
                JOptionPane.showMessageDialog(null,"You had "+i+" failed attempts","Error",JOptionPane.ERROR_MESSAGE);
            }
        } catch (IOException ex) {

        }
        }

  System.exit(0);











    }



}



public boolean isValidCred(String user, String password) throws FileNotFoundException, IOException
{

    user = usernameTF.getText();
    password = pass.getText();
     File file=new File("C:\login\user.txt");
     File file2=new File("C:\login\pass.txt");

        BufferedReader in = new BufferedReader(new FileReader(file));
        String line=in.readLine();
        BufferedReader in2 = new BufferedReader(new FileReader(file2));
        String line2=in2.readLine();


    if (user.equals(line) && password.equals(line2))
    {
        return true;
    }
    else
    {
        return false;
    }
    }




private class CancelButtonHandler implements ActionListener{
    @Override
    public void actionPerformed (ActionEvent e){
        dispose();
    }
}

    public static void main(String[] args) {
        Password pass=new Password();
    }

}

您不希望 LogInButtonHandleractionPerformed 内有循环,因为该循环在第一次失败后不会接受新输入。

相反,每次按下登录按钮时,您应该增加一些成员变量。 actionPerformed of LogInButtonHandler 将测试该变量的值以确定向用户显示哪条消息。

你可能需要这样的东西:

private class LogInButtonHandler implements ActionListener
{
    int i = 0;
    @Override
    public void actionPerformed(ActionEvent e) {

    String password=null;
    String user=null;
    boolean isValid;

    if (i < 3) {
        isValid=isValidCred(user,password);
        if(isValid) {
            JOptionPane.showMessageDialog(null,"Welcome "+user,"Succesful",JOptionPane.INFORMATION_MESSAGE );
            return;
        }
        i++;
    }
    JOptionPane.showMessageDialog(null,"Username/Password Inccorect Please Try Again: "+i,"Error",JOptionPane.ERROR_MESSAGE);

    if(i==2) {
        JOptionPane.showMessageDialog(null,"This " +(++i) + "rd login will be your final attempt","Error",JOptionPane.ERROR_MESSAGE);
    } else {
        JOptionPane.showMessageDialog(null,"You had "+i+" failed attempts","Error",JOptionPane.ERROR_MESSAGE);
    }

}