如果布尔变量为true,如何获取要打印的程序?

时间:2018-11-16 05:31:46

标签: java if-statement methods boolean

我知道这似乎是一个非常简单的问题,但是请记住,我是Java新手。附带的是我的源代码,最后写了if语句,如果变量“ valid”为true,则打印出“ Password is valid”。该代码可以正常运行,直到达到这一点为止,此时退出程序而不是打印“ password有效”。我查看了堆栈溢出中的多个线程,以了解如何解决该问题,并且大多数线程建议该代码可以正常工作。任何意见,将不胜感激。谢谢

import java.util.Scanner;

public class PasswordValidator {

public static void main(String[] args) {
    //declare name and pass and insert scanner
    Scanner sc = new Scanner(System.in);
    String name = "name";
    String pass = "pass";

    // tell user to type name and password and store  as variables
    System.out.print("Enter user name: ");
    name = sc.nextLine();
    check(name);  // check if name is equal to -1
    System.out.print("Enter password: ");
    pass = sc.nextLine();
    validate(name,pass); // call method 
}

static boolean validate(String userName, String password) {
    //declare necessary variables
    String upperCase = ".*[A-Z].*";
    String lowerCase = ".*[a-z].*";
    String number = ".*[0-9].*";
    String special = ".*[^A-Za-z0-9 ].*";
    boolean valid = true;

    if (password.matches("-1")) { // if input for password is -1 exit program
        System.exit(0);
    }
    if (password.matches(upperCase)) {  // check to see if input has one upper case letter  
        valid = true;
    }
        else  
            System.out.println("Password should contain at least one upper-case alphabet.");
            valid = false;
    if(password.matches(lowerCase)) {  // check to see if input has one lower case letter
        valid = true;
    }
        else 
            System.out.println("Password should contain at least one lower-case alphabet.");
            valid = false;  
    if (password.matches(number)) { // check to see if input has one number
        valid = true;
    }
        else
            System.out.println("Password should contain at least one number.");
            valid = false;      
    if(password.matches(special)) { // check to see if input has a special char.
        valid = true;
    }
        else
            System.out.println("Password should contain at least one special character.");
            valid = false;      
    if(password.length()>=7 && password.length() <= 10) { // make sure the password input = 7-10 char.
        valid = true;
    }
        else 
            System.out.println("Password should be within 7 to 10 characters in length.");  
            valid = false;  

    if (password.matches(".*\\s.*")) { // check to see if input has white spaces
        System.out.println("Password should not contain whitespace.");
        valid = false;
    }

    if(password.matches(userName))  { // give error if user name and password are the same
        System.out.println("Password should not contain or be the same as username.");
        valid = false;
    }
    // this is where I try to print if password is valid. for some reason my program just exits without printing :(
    if(valid==true) {
        System.out.print("Password is valid");
    }

    return valid;

}

// method used to check if user name is -1, and close program if so
static boolean check(String userName) {
    if (userName.matches("-1")){
            System.exit(0);
    }
    return true;

}
}

3 个答案:

答案 0 :(得分:1)

这里的问题是大括号。您在else中打印一些语句,然后不管条件为true还是false,都将有效标志设置为false。另外,最后关闭扫描仪。另外,您无需检查是否valid == true,只需将其置于if条件即可。您的代码看起来像

package com.digit.main;

import java.util.Scanner;

public class PasswordValidator {

public static void main(String[] args) {
    // declare name and pass and insert scanner
    Scanner sc = new Scanner(System.in);
    String name = "name";
    String pass = "pass";

    // tell user to type name and password and store as variables
    System.out.print("Enter user name: ");
    name = sc.nextLine();
    check(name); // check if name is equal to -1
    System.out.print("Enter password: ");
    pass = sc.nextLine();
    validate(name, pass); // call method
    sc.close();
}

static boolean validate(String userName, String password) {
    // declare necessary variables
    String upperCase = ".*[A-Z].*";
    String lowerCase = ".*[a-z].*";
    String number = ".*[0-9].*";
    String special = ".*[^A-Za-z0-9 ].*";
    boolean valid = true;

    if (password.matches("-1")) { // if input for password is -1 exit program
        System.exit(0);
    }
    if (password.matches(upperCase)) { // check to see if input has one upper case letter
        valid = true;
    } else {
        System.out.println("Password should contain at least one upper-case alphabet.");
        valid = false;
    }
    if (password.matches(lowerCase)) { // check to see if input has one lower case letter
        valid = true;
    } else {
        System.out.println("Password should contain at least one lower-case alphabet.");
        valid = false;
    }
    if (password.matches(number)) { // check to see if input has one number
        valid = true;
    } else {
        System.out.println("Password should contain at least one number.");
        valid = false;
    }
    if (password.matches(special)) { // check to see if input has a special char.
        valid = true;
    } else {
        System.out.println("Password should contain at least one special character.");
        valid = false;
    }
    if (password.length() >= 7 && password.length() <= 10) { // make sure the password input = 7-10 char.
        valid = true;
    } else {
        System.out.println("Password should be within 7 to 10 characters in length.");
        valid = false;
    }

    if (password.matches(".*\\s.*")) { // check to see if input has white spaces
        System.out.println("Password should not contain whitespace.");
        valid = false;
    }

    if (password.matches(userName)) { // give error if user name and password are the same
        System.out.println("Password should not contain or be the same as username.");
        valid = false;
    }
    // this is where I try to print if password is valid. for some reason my program
    // just exits without printing :(
    if (valid) {
        System.out.print("Password is valid");
    }

    return valid;

}

// method used to check if user name is -1, and close program if so
static boolean check(String userName) {
    if (userName.matches("-1")) {
        System.exit(0);
    }
    return true;

}

}

答案 1 :(得分:0)

您的module github.com/foo/bar replace labix.org/v2/mgo => github.com/globalsign/mgo v0.0.0-20181015145952-eeefdecb41b842af6dc652aaea4026e8403e62df require ( github.com/DATA-DOG/godog v0.7.8 github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38 // indirect github.com/alecthomas/colour v0.0.0-20160524082231-60882d9e2721 // indirect github.com/alecthomas/repr v0.0.0-20181024024818-d37bc2a10ba1 // indirect ... ) 子句不包含括号,因此您多次将else设置为valid。另外,一旦测试失败,您也不想将false设置回valid。解决这两个问题应该会给您带来类似的效果,

true

答案 2 :(得分:0)

您应该将所有uploadImage=(e)=>{ let file = e.target.files[0]; if (file && !file.name) { window.alert("Please select a file"); return false; } if (file.size > 10e6) { window.alert("Please upload a file smaller than 10 MB"); return false; } } 部件都放在方括号内,

例如:

代替使用此

else

请像这样使用它

else 
    System.out.println("Password should be within 7 to 10 characters in length.");  
    valid = false;  

您应该对所有else { System.out.println("Password should be within 7 to 10 characters in length."); valid = false; } 部分执行此操作,并且应该可以使用。

如果使用不带方括号的else,则仅else之后的第一行将位于else之内。那就是您在这里所做的错误。此外,如果您仔细了解了代码的执行方式,则可以通过始终删除不必要的false和true分配来改进代码