Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 提示用户输入密码并检查条件的程序_Java_String_Eclipse_If Statement_Boolean - Fatal编程技术网

Java 提示用户输入密码并检查条件的程序

Java 提示用户输入密码并检查条件的程序,java,string,eclipse,if-statement,boolean,Java,String,Eclipse,If Statement,Boolean,所以我一直在做这个程序,它会提示用户输入密码,但密码必须至少有8个字符长,并且只包含字母和数字。这是我的密码: import java.util.Scanner; public class Password { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.p

所以我一直在做这个程序,它会提示用户输入密码,但密码必须至少有8个字符长,并且只包含字母和数字。这是我的密码:

import java.util.Scanner;

public class Password {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
    System.out.println("Please enter a password: ");
    String password = input.nextLine();
    boolean valid = true;

    if (valid(password)) {
      System.out.println("Password accepted!");
    }
  }

  public static boolean valid(String password) {
    if (password.length() < 8) {
      System.out.println("Password must have at least eight characters.");
      return false;
    } else {
    char c;
    for (int i = 0; i < password.length(); i++) {
      c = password.charAt(i);
      if (password.matches("[0-9a-zA-Z]+")) {
        return true;
      } else {
        System.out.println("Password must only contain letters and digits.");
        return false;
      } 
    }
    return true;
    }
  }
}
import java.util.Scanner;
公共类密码{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入密码:”);
字符串密码=input.nextLine();
布尔有效=真;
if(有效(密码)){
System.out.println(“接受密码!”);
}
}
公共静态布尔值有效(字符串密码){
if(password.length()<8){
System.out.println(“密码必须至少有八个字符。”);
返回false;
}否则{
字符c;
对于(int i=0;i
我遇到的问题是,当我输入$$$这样的输入时,唯一的输出是“密码必须至少包含八个字符”,但我需要此输出和“”密码必须仅包含字母和数字。“换句话说,我需要程序检查两个条件是否都为假,并显示两个输出。我该怎么做呢

另外,如果你对如何改进我的程序有任何建议,那也会很有帮助


谢谢大家!

您可以简单地将有效变量从main方法移动到valid方法,并针对每个不满足的条件将其设置为false。只有在检查完所有条件后才能返回

public static boolean checkValid(String password) {
    boolean valid = true;

    if (password.length() < 8) {
        System.out.println("Password must have at least eight characters.");
        valid = false;
    }
    if(!password.matches("[0-9a-zA-Z]+")) {
        System.out.println("Password must only contain letters and digits.");
        valid = false;
    }

    return valid;
}
公共静态布尔校验有效(字符串密码){
布尔有效=真;
if(password.length()<8){
System.out.println(“密码必须至少有八个字符。”);
有效=错误;
}
如果(!password.matches(“[0-9a-zA-Z]+”){
System.out.println(“密码必须只包含字母和数字。”);
有效=错误;
}
返回有效;
}

您不需要遍历密码中的每个字符来匹配正则表达式。

else
语句中添加一个条件来检查输入的长度。@Mena我会尝试的,谢谢!我会这么做,但我的部分任务是将布尔变量设置为true。您仍然将布尔变量设置为true,但这样您实际上可以使用它。没有理由把有效变量放在你有的地方,因为你没有用它做任何事情。哦,我明白你的意思。非常感谢!