首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将布尔值转换为消息?

如何将布尔值转换为消息?
EN

Stack Overflow用户
提问于 2013-11-18 06:30:47
回答 2查看 96关注 0票数 1

基本上,我所做的程序是一个密码,限制为6-10个字符,并且至少需要一个字符和一个密码。如何将布尔值转换为一条消息。我正在为Japplet使用另一个类,这就是为什么我想传递它的原因。

编辑:,当我的另一个类获得用户输入时,我不知道如何做到这一点。

代码语言:javascript
复制
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;
        }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-18 06:33:50

您需要检查您的密码,并相应地分配您的消息。

代码语言:javascript
复制
if( Password.check(null) == true) // This just passes null instead of the password string to the check method.

您需要将实际密码字符串发送到check方法,而不是null。

代码语言:javascript
复制
if( Password.check(yourPasswordString) == true) // Send your password value got from the user

为了进一步简化它,您可以删除==检查。

代码语言:javascript
复制
if( Password.check(yourPasswordString))

为了进一步简化它,您可以使用David提到的三元操作符,如下所示

代码语言:javascript
复制
String myMessage = Password.check(yourPasswordString) ? "Your password is valid" : "You failed";

编辑:

看到编辑后,这就是您需要做的。

将布尔值结果传递给getDisplay()方法。

代码语言:javascript
复制
// 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方法并这样调用它。

代码语言:javascript
复制
// 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";
}
票数 2
EN

Stack Overflow用户

发布于 2013-11-18 06:33:31

写些类似的东西

代码语言:javascript
复制
String myMessage = result ? "Your password is valid" : "You failed";

问号的意思是-看左边的布尔表达式。如果为真,则返回下一步的内容;如果为false,则返回冒号后面的内容。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20041481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档