基本上,我所做的程序是一个密码,限制为6-10个字符,并且至少需要一个字符和一个密码。如何将布尔值转换为一条消息。我正在为Japplet使用另一个类,这就是为什么我想传递它的原因。
编辑:,当我的另一个类获得用户输入时,我不知道如何做到这一点。
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;
}
}发布于 2013-11-18 06:33:50
您需要检查您的密码,并相应地分配您的消息。
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";
}发布于 2013-11-18 06:33:31
写些类似的东西
String myMessage = result ? "Your password is valid" : "You failed";问号的意思是-看左边的布尔表达式。如果为真,则返回下一步的内容;如果为false,则返回冒号后面的内容。
https://stackoverflow.com/questions/20041481
复制相似问题