我将如何编写一组字符串作为一组密码?
How would I program an array of Strings to work as a set of passwords?
所以我试图让一个 java 小程序接受一组多个密码,所以自然而然地我想把它们放在数组中。但是,数组中只有一个密码有效,即集合中的最后一个。 None 之前的那些会起作用,而我的小程序拒绝其他的。到目前为止,这是我的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JPasswordC extends JApplet implements ActionListener
{
private final String[] password = {"Rosebud", "Redrum", "Jason", "Surrender", "Dorothy"};
private Container con = getContentPane();
private JLabel passwordLabel = new JLabel("Password: ");
private JTextField passwordField = new JTextField(16);
private JLabel grantedPrompt = new JLabel("<html><font color=\"green\">Access Granted</font></html>");
private JLabel deniedPrompt = new JLabel("<html><font color=\"red\">Access Denied</font></html>");
public void init()
{
con.setLayout(new FlowLayout());
con.add(passwordLabel);
con.add(passwordField);
con.add(grantedPrompt);
grantedPrompt.setVisible(false);
con.add(deniedPrompt);
deniedPrompt.setVisible(false);
passwordField.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String input = passwordField.getText();
for(String p : password)
{
if(input.equalsIgnoreCase(p))
{
grantedPrompt.setVisible(true);
deniedPrompt.setVisible(false);
}
else
{
grantedPrompt.setVisible(false);
deniedPrompt.setVisible(true);
}
}
}
}
我怎样才能让它正常工作?我在做错数组吗?是代码里的东西吗?
即使找到有效密码,代码也会检查每个密码,这意味着即使找到有效密码,它仍会根据下一个密码的有效性进行更改。所以数组中的最后一个声明了 grantedPrompt
和 deniedPrompt
的状态。尝试在输入等于其中一个密码后添加一个 break
。
for(String p : password)
{
if(input.equalsIgnoreCase(p))
{
grantedPrompt.setVisible(true);
deniedPrompt.setVisible(false);
break; // break out or loop once found
}
else
{
grantedPrompt.setVisible(false);
deniedPrompt.setVisible(true);
}
}
您正在遍历所有密码,即使有 match.So 将代码更改为 return 密码匹配时的方法。
public void actionPerformed(ActionEvent ae)
{
String input = passwordField.getText();
for(String p : password)
{
if(input.equalsIgnoreCase(p))
{
grantedPrompt.setVisible(true);
deniedPrompt.setVisible(false);
return;
}
}
grantedPrompt.setVisible(false);
deniedPrompt.setVisible(true);
}
所以我试图让一个 java 小程序接受一组多个密码,所以自然而然地我想把它们放在数组中。但是,数组中只有一个密码有效,即集合中的最后一个。 None 之前的那些会起作用,而我的小程序拒绝其他的。到目前为止,这是我的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JPasswordC extends JApplet implements ActionListener
{
private final String[] password = {"Rosebud", "Redrum", "Jason", "Surrender", "Dorothy"};
private Container con = getContentPane();
private JLabel passwordLabel = new JLabel("Password: ");
private JTextField passwordField = new JTextField(16);
private JLabel grantedPrompt = new JLabel("<html><font color=\"green\">Access Granted</font></html>");
private JLabel deniedPrompt = new JLabel("<html><font color=\"red\">Access Denied</font></html>");
public void init()
{
con.setLayout(new FlowLayout());
con.add(passwordLabel);
con.add(passwordField);
con.add(grantedPrompt);
grantedPrompt.setVisible(false);
con.add(deniedPrompt);
deniedPrompt.setVisible(false);
passwordField.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String input = passwordField.getText();
for(String p : password)
{
if(input.equalsIgnoreCase(p))
{
grantedPrompt.setVisible(true);
deniedPrompt.setVisible(false);
}
else
{
grantedPrompt.setVisible(false);
deniedPrompt.setVisible(true);
}
}
}
}
我怎样才能让它正常工作?我在做错数组吗?是代码里的东西吗?
即使找到有效密码,代码也会检查每个密码,这意味着即使找到有效密码,它仍会根据下一个密码的有效性进行更改。所以数组中的最后一个声明了 grantedPrompt
和 deniedPrompt
的状态。尝试在输入等于其中一个密码后添加一个 break
。
for(String p : password)
{
if(input.equalsIgnoreCase(p))
{
grantedPrompt.setVisible(true);
deniedPrompt.setVisible(false);
break; // break out or loop once found
}
else
{
grantedPrompt.setVisible(false);
deniedPrompt.setVisible(true);
}
}
您正在遍历所有密码,即使有 match.So 将代码更改为 return 密码匹配时的方法。
public void actionPerformed(ActionEvent ae)
{
String input = passwordField.getText();
for(String p : password)
{
if(input.equalsIgnoreCase(p))
{
grantedPrompt.setVisible(true);
deniedPrompt.setVisible(false);
return;
}
}
grantedPrompt.setVisible(false);
deniedPrompt.setVisible(true);
}