[英]How to get program to print, if a boolean variable is true?
我知道這似乎是一個非常簡單的問題,但請記住,我是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;
}
}
這里的問題是括號。 您在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;
}
}
您的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
部分放在方括號中,
例如:
而不是使用這個
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
后面的第一行將位於else
。 那就是您在這里所做的錯誤。 此外,如果您仔細了解了代碼的執行方式,則可以通過始終刪除不必要的false和true分配來改進代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.