密码验证程序Java

时间:2016-11-17 16:18:16

标签: java

我目前正在为我的Java课程开展最后的项目,我需要一些帮助!首先,我将向您展示说明,我的代码,然后是我的问题。

说明

  1. 编写一个程序,提示用户输入密码。
  2. 创建一个名为valid的布尔变量并将其设置为true。如果以下任何测试失败,请将其设置为true。
  3. 检查密码以查看密码是否至少包含8个字符。如果没有,则显示消息“密码必须至少包含8个字符”
  4. 检查密码,看它是否只包含字母和数字。为此,您需要遍历字符串中的所有字符。如果此表达式为真,则字符c是一个数字字母:

    ('a'< = c&& c< ='z')|| ('A'< = c&& c< ='Z')|| ('0'< = c&& c< ='9')

  5. 如果这不是真的,请从循环中断并显示消息“密码必须只包含字母和数字” 5.如果在程序结束时仍然有效,则显示消息“密码接受!”

    我的代码

    import java.util.Scanner; 
    public class PasswordVerification {
    
        public static void main(String[] args) {
    //      Creates a scanner
            Scanner sc = new Scanner(System.in);
            boolean valid = false; 
            String password;
    
    
    //      Asks user to enter password
            System.out.print("Please enter password and then hit enter:");
            password = sc.nextLine(); 
    
    //      Checks to see if password is at least 8 characters. 
            if (password.length()<8) 
                {
                    valid = false;
                    System.out.println("Password must have at least 8 characters");
                }
    
    //      Checks each character to see if it is acceptable.
            for (int i = 0; i < password.length(); i++){
                        char c = password.charAt(i);
    
                        if (       ('a' <= c && c <= 'z') // Checks if it is a lower case letter
                                || ('A' <= c && c <= 'Z') //Checks if it is an upper case letter
                                || ('0' <= c && c <= '9') //Checks to see if it is a digit
                        ) 
                        {
    
                            valid = true;
                        } 
    
                        else 
                        {
                            // tells the user that only letters & digits are allowed
                            System.out.println("Only letter & digits are acceptable.");
                            valid = false;
                            break;
                        }
    
            }
    
            // if the password is valid, tell the user it's accepted
            System.out.println("Password Accepted");
            }
    
    
    }
    

    所以我到目前为止有说明和我的代码。我很接近完成我只是遇到第四部分的问题。所以预期的输出应该是:

    Please enter a password: abc
    Password much have at least 8 characters 
    Please enter a password: abcd1234$
    Password must only contain letter and digits
    Please enter a password: ####
    Password must have at least 8 characters
    Password must only contain letters and digits
    Please enter a password: abcd1234
    Password accepted!
    

    当我输入abc时,这就是我得到的:

    Please enter password and then hit enter:abc
    Password must have at least 8 characters
    Password Accepted
    

    当我这样做时,程序结束了!有人可以帮助我吗?提前致谢。 =)另外,这是一个Java Programming I类。

4 个答案:

答案 0 :(得分:2)

正如@cralfaro所说,如果密码无效,你必须重复这个过程:

import java.util.Scanner; 
public class PasswordVerification {

    public static void main(String[] args) {
        //      Creates a scanner
        Scanner sc = new Scanner(System.in);
        boolean valid = false; 
        String password;

        do { // start a loop
            //      Asks user to enter password
            System.out.print("Please enter password and then hit enter:");
            password = sc.nextLine(); 

            //      Checks to see if password is at least 8 characters. 
            if (password.length()<8) 
            {
                valid = false;
                System.out.println("Password must have at least 8 characters");
                continue; // skip to next iteration
            }

            //      Checks each character to see if it is acceptable.
            for (int i = 0; i < password.length(); i++){
                char c = password.charAt(i);

                if (       ('a' <= c && c <= 'z') // Checks if it is a lower case letter
                        || ('A' <= c && c <= 'Z') //Checks if it is an upper case letter
                        || ('0' <= c && c <= '9') //Checks to see if it is a digit
                ) 
                {

                    valid = true;
                }
                else 
                {
                    // tells the user that only letters & digits are allowed
                    System.out.println("Only letter & digits are acceptable.");
                    valid = false;
                    break;
                }

            }
        } while(!valid); // verify if the password is valid, if not repeat the process

        // if the password is valid, tell the user it's accepted
        System.out.println("Password Accepted");
    }


}

通过这种方式,如果密码无效,程序将继续询问用户输入。

修改 感谢GC_的评论,问题是我错过了第一次检查中的继续声明。

答案 1 :(得分:0)

你的问题是这部分代码:

onUserSelectedOption = (option, index) => {
  this.setState({
      userSelectedIndex: index
   });
  if (option.correct === 1) {
   //do something
  }else {
   //do something
  }
}
render(){
    const result = this.state.data.map((data) =>{
    const xx = data.option.map((option, index)=>{
    return (
      <TouchableHighlight onPress={() => this.onUserSelectedOption(option, index)}>
        <Text key={option.option_id} style={{backgroundColor: index === this.state.userSelectedIndex ? 'red' : 'transparent' }}>{option.options}</Text>
      </TouchableHighlight>
    )
  })
  return(
    <View>
      <Text style={styles.titles}>{data.title}</Text>
      {xx}
      </View>
  )
})
return(
  <ScrollView>
    {result}
  </ScrollView>

);
}

此处您将if ( ('a' <= c && c <= 'z') // Checks if it is a lower case letter || ('A' <= c && c <= 'Z') //Checks if it is an upper case letter || ('0' <= c && c <= '9') //Checks to see if it is a digit ) { valid = true; } 重置为valid,即使长度小于true的第一次检查已失败。 因此8将失败abc并打印3.,这很好。然后它将通过Password must have at least 8 characters,重置有效至4.并打印true

所以你想要用{/ 1>替换Password Accepted部分

if (...) { valid = true; } else { ... }

修改: 另外,您最初应设置if (!( // if the character is none of the options below, print error ('a' <= c && c <= 'z') // Checks if it is a lower case letter || ('A' <= c && c <= 'Z') //Checks if it is an upper case letter || ('0' <= c && c <= '9') //Checks to see if it is a digit )) { // tells the user that only letters & digits are allowed System.out.println("Only letter & digits are acceptable."); valid = false; break; } 而不是boolean valid = true;,因为只有在条件失败时才将其设置为false。 然后在代码的末尾添加一个条件,检查最后一个输出行周围的false,如

valid

答案 2 :(得分:0)

有点乱,但试试这个:

pPoolSizes

答案 3 :(得分:-1)

在打印密码被接受之前,添加一项检查以查看密码是否仍然有效。

if(valid==true) {
    System.out.println("Password Accepted");
}

编辑:您也可以添加此内容。

else {
    System.out.println("Password Denied");
}