Java 如果布尔变量为真,如何让程序打印?

Java 如果布尔变量为真,如何让程序打印?,java,if-statement,methods,boolean,Java,If Statement,Methods,Boolean,我知道这似乎是一个非常简单的问题,但请记住,我是java新手。附件是我的源代码,最后我写了一个if语句,如果变量valid为true,则输出密码的if语句是有效的。代码运行正常,直到它到达这一点,在这一点上,它将退出程序,而不是打印有效的密码?我已经查看了大量堆栈上的线程溢出,以了解如何解决这一问题,大多数线程都建议此代码应该可以工作。如有任何建议,将不胜感激。谢谢 import java.util.Scanner; public class PasswordValidator { publ

我知道这似乎是一个非常简单的问题,但请记住,我是java新手。附件是我的源代码,最后我写了一个if语句,如果变量valid为true,则输出密码的if语句是有效的。代码运行正常,直到它到达这一点,在这一点上,它将退出程序,而不是打印有效的密码?我已经查看了大量堆栈上的线程溢出,以了解如何解决这一问题,大多数线程都建议此代码应该可以工作。如有任何建议,将不胜感激。谢谢

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;

}
}
else子句不包含大括号,因此多次盲目地将valid设置为false。此外,您不希望在valid测试失败后将其设置回true。解决这两个问题应该会让你

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.equals("-1")) {
        System.exit(0);
    }
    if (!password.matches(upperCase)) { // check to see if input has one upper case letter
        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
        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
        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.
        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.
        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;
    }
    if (valid) {
        System.out.println("Password is valid");
    }

    return valid;
}
你应该把所有其他的部分都放在括号里

例:

而不是用这个

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的不必要的赋值来改进代码。这里的问题是,在else之后加上大括号。您在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;

}

}

ifvalidatename,传递{//do you want to next}您是否在该点进行调试并检查了valid的值,因为我认为它将为false,这就是代码退出的原因。当您退出时,System.out.printlnPassword应至少包含一个特殊字符。;有效=错误;这样做,它将始终将您的有效varuiable更改为false,在else子句中使用大括号。。。它们只涉及一项声明。您多次将valid设置为false。此外,如果valid为false,则不应将其设置为true。你在这里有一些逻辑问题需要克服。请看@ElliottFrisch评论我觉得这是我犯的一个简单的错误。把牙套套在牙套上就成功了。即使多次更改valid的值,它现在也可以正常运行。我想我不明白为什么多次将“有效”从“真”更改为“假”是一个问题。@Cole一个全小写的密码不应该是有效的。您的代码将显示一条消息,将valid设置为false无大写字母,然后确定是否有小写字母,以便密码再次有效。