我所做的程序基本上是用于将密码限制为6-10个字符并且至少需要1个字符和1个密码的密码。如何获取布尔值并将其转换为消息。我正在为Japplet UI使用另一个类,这就是我想要传递它的原因。
编辑:当我的其他课程获得用户输入时,我不明白该怎么做。
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class PasswordUI extends JApplet implements ActionListener {
JLabel passwordLabel = new JLabel("");
JTextField inputBox = new JTextField(40);
JButton goButton = new JButton("Go");
Container con = getContentPane();
public void init()
{
con.setLayout(new FlowLayout());
con.add(new JLabel("Enter a password between 6-10 characters. Must contain atleast 1 number and 1 letter"));
con.add(inputBox);
con.add(goButton);
con.add(passwordLabel);
goButton.addActionListener(this);
inputBox.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String userInput = inputBox.getText();
boolean pass= Password.check(userInput);
//String message = Password.display(null);
Password mess= new Password();
passwordLabel.setText( mess.getDisplay());
}
}
public class Password {
public static boolean check(String str)
{
boolean result = false;
char ch;
int letterCount = 0,
digitCount= 0 ;
int length = str.length();
if(str.length()>= 6)
{
if(str.length()<= 10)
{
for (int i=0; i<length; i++ ) //i<end
{
ch = str.charAt(i);
if (Character.isDigit(ch))
digitCount++;
else if (Character.isLetter(ch))
letterCount++;
}
if (letterCount >= 1 && digitCount >= 1)
result = true;
}
}
return result;
}
public String getDisplay()
{
String message = "";
if( Password.check(null) == true) This is what I'm having trouble whit
{
message = "The Password meets the requirements";
}
else
{
message = "Your password does not match the requirements";
}
return message;
}
}
答案 0 :(得分:2)
写点像
String myMessage = result ? "Your password is valid" : "You failed";
问号意味着 - 查看左侧的布尔表达式。如果这是真的,那就回报下一个;如果它是假的,返回结肠后的内容。
答案 1 :(得分:2)
您需要检查密码并相应地分配您的信息。
if( Password.check(null) == true) // This just passes null instead of the password string to the check method.
您需要将实际密码字符串发送到check方法,而不是null。
if( Password.check(yourPasswordString) == true) // Send your password value got from the user
为了进一步简化,您可以删除==
支票。
if( Password.check(yourPasswordString))
为了进一步简化,您可以使用三元运算符David提及此类
String myMessage = Password.check(yourPasswordString) ? "Your password is valid" : "You failed";
修改强>
看到你的编辑后,这就是你需要做的。
将布尔结果传递给getDisplay()
方法。
// inside init() method
boolean pass= Password.check(userInput);
Password mess= new Password();
passwordLabel.setText(mess.getDisplay(pass)); // passing the boolean to the getDisplay method
...
// getDisplay method.
public String getDisplay(boolean result) {
return result ? "The Password meets the requirements" : "Your password does not match the requirements";
}
您甚至可以将getDisplay()
作为static
方法,并将其称为此类。
// inside init() method
boolean pass= Password.check(userInput);
// static method, hence mess object is not required.
passwordLabel.setText(Password.getDisplay(pass)); // passing the boolean to the getDisplay method
...
// getDisplay method.
public static String getDisplay(boolean result) { // static method
return result ? "The Password meets the requirements" : "Your password does not match the requirements";
}