完整&半密码 [英] Full & half password

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

问题描述

如何编写Java程序提示用户输入密码并将其显示在控制台窗口中。显示的密码应该是输入的密码减半,最后添加完整密码的总字符数。



example1:

输入你的密码:abcd

您的密码是:ab4

example2:

输入您的密码:abcde

您的密码是:ab5



注意:始终假设用户输入的密码至少包含两个字符。此外,如果密码长度为奇数,则无需考虑这些情况(例如:如果密码长度为5,则程序应仅显示前两个字符以及全长)。



我尝试了什么:



How do I write a Java Program That prompts the user to enter a password and displays it back in the console window. The displayed password should be the entered password halved with the full password total number of characters added at the end.

example1:
enter your password: abcd
your password is : ab4
example2:
enter your password: abcde
your password is :ab5

Note: always assume that the user will enter a password with at least two characters. Additionally, you don’t need to consider the cases if the password length is an odd number (e.g: if the password length is 5 the program should only display the first two characters along with the full length).

What I have tried:

import java.util.Scanner;

public class Mood {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s1 = sc.nextLine();
        int n = UserMainCode.display(s1);
        if(n==1){
         System.out.println("Valid password");
        }else{
         System.out.println("Invalid password");
        }

  }
}

class UserMainCode {
   
    
    public static int display(String password){
if(password.matches(".*[a-z]{a,}.*") && password.matches(".*[@#$]{1,}.*") && password.length()>=4 && password.length()<=20)
                    {
                                    return 1;
                    }
                    else
                    {
                                    return -1;
                    }
       }

    }

推荐答案

{1,}。* )&&& password.length()> = 4&& password.length()< = 20)
{
return 1 ;
}
else
{
返回 - 1 ;
}
}

}
{1,}.*") && password.length()>=4 && password.length()<=20) { return 1; } else { return -1; } } }


这篇关于完整&amp;半密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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