密码验证(布尔方法) [英] Password validation ( boolean methode)

查看:138
本文介绍了密码验证(布尔方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个布尔方法,它获取一个字符串并验证它是否至少有10个字符,一个大写,一个小写,一个数字,只有字母数字,并返回布尔值true或false,在main方法中我们可以检查答案



我尝试过:



write a Boolean method which gets a string and verify if it has at least 10 character , one uppercase , one lowercase , one number, and only alphanumeric, and return Boolean true or false , in the main method we can check the answer

What I have tried:

import java.util.Scanner;
public class PasswordValidator {
  public static void main (String[] args){
    Scanner input= new Scanner (System.in);
    //System.out.println
      boolean answer1 =(isSecurePassword( input.nextLine()));
      System.out.println(answer1);
    /*String pass1 = "This is a secure password";
    String pass2 = "This is not a secure password";
    
    boolean answer1 = isSecurePassword(pass1);
    boolean answer2 = isSecurePassword(pass2);
    
    System.out.println("answer1 = " + answer1);
    System.out.println("answer2 = " + answer2);*/

    
  }
  public static boolean isSecurePassword (String password){
  
   
    int i;
    for (i=0;i<password.length();i++){
      
    if (i!=10)
    {
   
    return false;
    
    }
    
      
  }
  
  }
}

推荐答案

阅读问题:这很明显你需要做什么。

1)读入字符串。

2)检查它有10个字符。如果没有,则返回false。

3)声明三个布尔变量:hasUpperCase,hasLowerCase,hasDigit并将它们全部设置为false。

4)循环字符串,依次查看每个字符。

4.1)如果它是大写,则将hasUpperCase设置为true。

4.2)否则,如果它是小写,则将hasLowerCase设置为true。 />
4.3)否则,如果它是一个数字,将hasDigit设置为true。

4.4)否则,返回false;

5)循环后,返回hasUpperCase AND hasLowerCase AND hasDigit
Read the question: it's makes it pretty obvious what you have to do.
1) Read in the string.
2) Check it has 10 characters. If it doesn't, return false.
3) Declare three boolean variables: hasUpperCase, hasLowerCase, hasDigit and set them all to false.
4) Loop through the string, looking at each character in turn.
4.1) If it's Uppercase, set hasUpperCase to true.
4.2) Otherwise, if it's lowercase, set hasLowerCase to true.
4.3) Otherwise, if it's a digit, set hasDigit to true.
4.4) Otherwise, return false;
5) After the loop, return hasUpperCase AND hasLowerCase AND hasDigit


Yor实现(注释中的那个)充满了bug。希望我已经修好了所有这些。尝试

Yor implementation (the one in the comment) is full of bugs. Hopefully I've fixed all of them. Try
import java.util.Scanner;

public class PasswordValidator
{
  public static void main (String[] args)
  {
    Scanner input = new Scanner (System.in);
    boolean isSecure =isSecurePassword( input.nextLine());
    System.out.println(isSecure);
  }

  public static boolean isSecurePassword (String password)
  {
    if ( password.length() < 10) return false;

    boolean uppercase=false;
    boolean lowercase=false;
    boolean digit=false;

    for(char c : password.toCharArray())
    {
      if (c >='a'&& c <='z')
        lowercase = true;
      else if (c >='A'&& c <='Z')
        uppercase =true;
      else if ( c >='0' && c<='9')
        digit = true;
    }
    return (uppercase && lowercase && digit);
  }
}


这篇关于密码验证(布尔方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆