sdut lava lab7.5

简介: sdut lava lab7.5

7-5 sdut-JAVA-Valid Password

分数 9

全屏浏览

切换布局

作者 马新娟

单位 山东理工大学

You have been requested to write a method that will be used when a user wishes to change his/her password. Your method should accept a String object and report whether or not this input constitutes a valid password. The rules for a valid password are as follows:

• between 6 to 10 characters in length

• must contain at least one digit, at least one uppercase alphabetic character (in accordance with the English alphabet), at least one lowercase alphabetic character (in accordance with the English alphabet) and at least one other non-space character (that is not a digit or alphabetic character in accordance with the English alphabet)

• no spaces

Input Specification:

Accept a String object .

Output Specification:

Report whether or not this input constitutes a valid password.

Sample Input:

Maggie26

Sample Output:

Password is invalid.
MAggie&26

Sample Output:

Password is valid.

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

栈限制

8192 KB

import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
          String arr =sc.nextLine();
         if(arr.length()<6||arr.length()>10){
          System.out.println("Password is invalid.");
          System.exit(0);
         }
         for(int i = 0 ; i < arr.length() ; i++){
          if(arr.charAt(i) == ' '){
            System.out.println("Password is invalid.");
            System.exit(0);
          }
         }
         int min = 0;
         int max = 0;
         int di = 0;
         int fu = 0;
         for(int i = 0 ;i  <arr.length();i++){
          if(arr.charAt(i)>='0'&&arr.charAt(i)<='9'){
            di+=1;
          }
          else if(arr.charAt(i)>='A'&&arr.charAt(i)<='Z'){
            max+=1;
          }
          else if(arr.charAt(i)>='a'&&arr.charAt(i)<='z'){
            min+=1;
          }
          else{
            fu += 1;
          }
         }
         
         if(min>=1&&max>=1&&di>=1&&fu>=1){
          System.out.println("Password is valid.");
         }
         else{
          System.out.println("Password is invalid.");
         }
       
}
}

Java代码的目的是验证用户输入的字符串(密码)是否符合一定的规则。以下是对每一行代码的解释:

import java.util.Scanner;

导入Java的Scanner类,用于读取用户的输入。

public class Main {

定义了一个名为Main的公共类。

public static void main(String[] args) {

定义了程序的主入口点main方法。

Scanner sc = new Scanner(System.in);

创建了Scanner类的一个实例sc,用于从标准输入读取数据。

String arr = sc.nextLine();

读取用户输入的一行文本,通常是一个密码,并将其存储在字符串变量arr中。

if (arr.length() < 6 || arr.length() > 10) {

检查密码的长度是否小于6或大于10。

System.out.println("Password is invalid.");

如果长度不符合要求,打印一条消息并退出程序。

System.exit(0); }

结束if语句,并正常退出程序。

for (int i = 0; i < arr.length(); i++) {

遍历密码字符串的每个字符。

if (arr.charAt(i) == ' ') {

检查当前字符是否是空格。

System.out.println("Password is invalid.");

如果密码中包含空格,打印一条消息并退出程序。

System.exit(0);

结束if语句,并正常退出程序。

}

结束for循环中的if语句。

}

结束for循环。

int min = 0; int max = 0; int di = 0; int fu = 0;

初始化四个计数器变量,用于统计不同类型的字符。

for (int i = 0; i < arr.length(); i++) {

再次遍历密码字符串的每个字符,这次是为了统计字符类型。

if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {

检查当前字符是否是数字。

di += 1;

如果是数字,增加数字计数器di

} else if (arr.charAt(i) >= 'A' && arr.charAt(i) <= 'Z') {

检查当前字符是否是大写字母。

max += 1;

如果是大写字母,增加大写字母计数器max

} else if (arr.charAt(i) >= 'a' && arr.charAt(i) <= 'z') {

检查当前字符是否是小写字母。

min += 1;

如果是小写字母,增加小写字母计数器min

} else {

如果当前字符不是字母、数字或下划线。

fu += 1;

增加特殊字符计数器fu

}

结束if-else语句。

}

结束for循环。

if (min >= 1 && max >= 1 && di >= 1 && fu >= 1) {

检查是否至少有一个小写字母、一个大写字母、一个数字和一个特殊字符。

System.out.println("Password is valid.");

如果所有条件都满足,打印一条消息表明密码有效。

} else {

否则,密码无效。

System.out.println("Password is invalid.");

打印一条消息表明密码无效。

}

结束if-else语句。

}

结束main方法。

}

结束Main类。

这个程序的逻辑是:首先检查密码的长度是否在6到10个字符之间,然后检查密码中是否包含空格。接着,统计密码中小写字母、大写字母、数字和其他字符的数量。最后,检查是否每种类型的字符至少有一个,以此来判断密码是否有效。


目录
相关文章
|
1月前
|
安全
sqli-lab教程Less-9
sqli-lab教程Less-9
36 0
|
1月前
|
安全 数据安全/隐私保护
sqli-lab教程Less-4
sqli-lab教程Less-4
21 0
|
1月前
|
关系型数据库 MySQL Shell
sqli-lab教程Less-7
sqli-lab教程Less-7
37 0
|
1月前
|
Java
SDUT lab 1.3
SDUT lab 1.3
28 0
|
1月前
|
计算机视觉
Lab
Lab
20 1
|
1月前
SDUT lab5-2
SDUT lab5-2
20 0
|
1月前
|
Java
sdut lab1.4
sdut lab1.4
25 0
|
1月前
|
安全 数据安全/隐私保护
sqli-lab教程Less-6
sqli-lab教程Less-6
42 0
|
监控 流计算
dc_labs--lab1的学习与总结
本节为dc_labs系列的第一篇,主要根据自己对于lab的理解,简述实验的过程,同时对于笔者自己觉得需要进一步理解的进行总结学习。本节重点在于理解启动文件与DC的综合流程。建议与对应博文([DC学习笔记正式篇之零——综述与基本流程介绍](https://guodongblog.com/posts/80912d01b675/))进行结合起来进行学习。该文为对应部分的实践篇的内容。本系列博文不会只是带着进行实验内容,个人觉得单纯跑一遍实验意义不大。会结合自己学习的理解进行部分展开。有问题欢迎留言一起学习。
769 0
|
IDE 开发工具 Python
Easy Games With Python and Pygame(一)- Pygame Quickstart
Easy Games With Python and Pygame(一)- Pygame Quickstart
Easy Games With Python and Pygame(一)- Pygame Quickstart