Java的$ C的字母和数字$ c检查密码? [英] Java code Check Password for Letter and Digit?

查看:181
本文介绍了Java的$ C的字母和数字$ c检查密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑 - VERSION
第一个职位是confusamagin。我的任务是创建一个密码提示程序。密码需要进行检查,看它是否确实具有至少一个数字,并在其中一个字母。此外,将密码长度必须在6之间 - 10。

我的问题是试图找出如何看一个数字和字母存在密码。在检查密码区,我不知道从哪里开始真的。我不知道怎么看它是否有一个字母和一个数字。我知道该怎么做还是通过使用for语句来计数和检查,但它所做的是检查,而它包含了所有字母或所有数字。

下面是我迄今为止...

 进口java.util.Scanner中;一流的密码{    公共静态无效的主要(字串[] args){
        扫描仪输入=新的扫描仪(System.in);// ------输入用户名
        的System.out.println(欢迎请输入您的用户名和密码。);
        System.out.print(用户名>>中);
        input.nextLine();
// ------密码验证BEGIN
        字符串密码= enterPassword();
            而(!checkPassword(密码)){
            的System.out.println(密码必须是6 - 10个字符长!);
            密码= enterPassword();
            }// ------密码验证
        串passwordverify = enterPassword();
        而(!password.equals(passwordverify)){
            的System.out.println(错误 - 密码不匹配密码重新输入);
            密码= enterPassword();        }// ------接受密码
        的System.out.println(用户名和密码已接受!);        }
// - 输入密码声明
    公共静态字符串enterPassword(){
        字符串密码;
        扫描仪输入=新的扫描仪(System.in);
        System.out.print(密码>>);
        密码= input.nextLine();
        返回密码;
        }// - 布尔检查PW
    公共静态布尔checkPassword(字符串密码){
        INT长;
        长度= password.length();
            如果(长度6; ||长度GT; 11){
            返回false;
            }
                    的for(int i = 0; I< password.length();我++){
        如果(!Character.isLetter(password.charAt(ⅰ)))
        返回false;
            }
            返回true;
        }}


解决方案

 公共静态布尔checkPasswordLetter(字符串密码){
            的for(int i = 0; I< password.length();){
            如果(!Character.isLetter(password.charAt(ⅰ))){
                返回false;
                }
            }
        返回true;
        }

在这里,你没有增加变量i,需要在我++或你的循环会永远如果不信,同样在checkPasswordDigit

EDIT--VERSION The first post was confusamagin. My assignment is to create a password prompt program. The password needs to be checked to see if it does have at least one digit and one letter in it. Also the password length must be between 6 - 10.

My problem is trying to figure out how see if a digit and letter exist the password. In the check password area I am not sure where to begin really. I am not sure how to see if it has a Letter and a Digit in one. I know how to do either or by using a for statement to count and check but all it does is check to see rather it contains all letters or all digits.

Below is what I have so far...

import java.util.Scanner;

class Password {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

//------ENTER A USERNAME
        System.out.println("Welcome please enter your username and password.");
        System.out.print("Username >>");
        input.nextLine();


//------PASSWORD AUTHENTICATION BEGIN       
        String password = enterPassword();
            while ( !checkPassword(password) ) {
            System.out.println("Password must be 6 - 10 characters long!"); 
            password = enterPassword();
            }

//------PASSWORD VERIFY
        String passwordverify = enterPassword();
        while (!password.equals(passwordverify)){
            System.out.println("ERROR - Passwords DO NOT MATCH Re-Enter Passwords Again");
            password = enterPassword();

        }

//------ACCEPT PASSWORD     
        System.out.println("Username and Password Accepted!");

        }


//--ENTER PASSWORD STATEMENT
    public static String enterPassword(){
        String password;
        Scanner input = new Scanner(System.in);
        System.out.print("Password >>");
        password = input.nextLine();
        return password;
        }

//--BOOLEAN CHECK PW    
    public static boolean checkPassword(String password){
        int length;
        length = password.length();
            if (length < 6 || length > 11){
            return false;
            }
                    for (int i = 0; i < password.length();i++){
        if (!Character.isLetter(password.charAt(i)))
        return false;
            }           
            return true;
        }

}

解决方案

  public static boolean checkPasswordLetter(String password){
            for (int i = 0; i < password.length();){
            if (!Character.isLetter(password.charAt(i))){
                return false;
                }
            }
        return true;
        }

Here you didn't increment variable i , need in for i++ or your loop is going forever if is not letter, same and in checkPasswordDigit

这篇关于Java的$ C的字母和数字$ c检查密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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