【问题标题】:Check for a specific set of characters(of a password) in a string检查字符串中的一组特定字符(密码)
【发布时间】:2015-06-06 13:02:52
【问题描述】:

我必须编写一个检查密码的程序。

  • 如果用户输入的密码是'bolt',会显示'The password is valid'
  • 否则将显示“The password is invalid”。

该程序仅适用于 if 部分,但不适用于 else
那就是当我输入 bolt 时,它会显示正确的消息。
但是当我输入 bolt 以外的内容时,它不会显示“The password is invalid”。
我被告知要测试使用 char.At 的所有四个字符。

import java.util.Scanner;

public class MyClass {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter your 4-character password:");
        String password = input.next();

        for (int i = 0; i < password.length(); i++) {
            if (password.charAt(i) == 'B' || password.charAt(i) == 'b')
                if (password.charAt(i + 1) == 'O'
                        || password.charAt(i + 1) == 'o')
                    if (password.charAt(i + 2) == 'L'
                            || password.charAt(i + 2) == 'l')
                        if (password.charAt(i + 3) == 'T'
                                || password.charAt(i + 3) == 't') {

                            System.out.println("The password is valid");
                        }
                        else {
                            System.out.println("The password is invalid!");

                        }
        }
}

【问题讨论】:

  • 我认为你的 else 只是处于错误的深度。
  • 任何使用某种形式的循环的解决方案都是低于标准的。如果您知道密码由 4 个字符组成,则应首先检查 null,然后检查字符串长度,然后才开始比较字符。此时您知道输入字符串是 4 个字符,因此不需要循环(这就是我在下面提出的解决方案所做的)。

标签: java string if-statement


【解决方案1】:

使用equalsIgnoreCase() 进行检查会更具可读性:

String password= input.next();
if(password.equalsIgnoreCase("bolt"){
   System.out.println("The password is valid");
}
else{
   System.out.println("The password is invalid!");
}

【讨论】:

  • 一个不相关的建议,使用"bolt".equalsIgnoreCase(password) 来避免NullPointerException in password
  • 当然……或者您可以随时验证空值。
【解决方案2】:

为什么不直接检查字符串是否相等?

String password= input.next();
if("bolt".equals(password))) {
  System.out.println("Valid password");
} else {
  System.out.println("InValid password");
}

如果您还认为BOLTBolt 有效,请使用equalsIgnoreCase()

如果你需要在没有equals 的情况下实现它,你可以使用这样的东西:

if (password != null && 
    password.length() == 4 && 
    (password.charAt(0) == 'B' || password.charAt(0) == 'b') && 
    ...) {
  System.out.println("Valid password");
} else {
  System.out.println("InValid password");
} 

【讨论】:

  • 我被告知要使用 char.At
  • 非常感谢!这很有帮助
【解决方案3】:

对于您更新的问题:

String password = input.next().toLowerCase();
String correctPassword = "bolt";

if (password.length() != correctPassword.length()) {
    System.out.println("Not valid");
    return;
}

for (int i = 0; i < correctPassword.length(); i++) {
    if (password.charAt(i) != correctPassword.charAt(i)) {
        System.out.println("Not valid");
        return;
    }
}
System.out.println("Valid");

在您的代码中有两个问题, 1. 您的 for 循环没有任何用途,因为您在第一次迭代中检查了整个密码。您的第二次迭代将导致IndexOutOfBoundsException

  1. 您没有看到无效的密码消息,因为您的 else 条件仅适用于最里面的 if(检查字符“T”或“t”)。
    因此,如果提供的密码以“BOL”开头,但最后一个字符与“BOLA”或“BOLB”不同,那么您将看到无效消息。但如果前三个字符失败,则不会执行 else。

希望这会有所帮助..

【讨论】:

  • 非常感谢!但是你能告诉我我的代码哪里出错了吗?我的意思是为什么 else 语句不起作用
  • 我已经更新了答案。如果需要更多解释,请告诉我。
  • 谢谢,我明白了。使用相同的方法,我怎样才能改进 for 循环和 else 语句以使其正常工作?
【解决方案4】:

我确实使用了接受正则表达式作为参数的string.matches 函数。 (?i) 有助于进行不区分大小写的匹配。

if(string.matches("(?i)bolt")) 
{
System.out.println("Valid password");
}
else {System.out.println("InValid password");}

【讨论】:

    【解决方案5】:

    你所做的没有多大意义。您可能应该使用字符数组然后按顺序检查。另一个小问题是else 块与最后一个if 语句相关联。它应该附加到第一个if 的位置。这是您需要智能使用牙套的地方。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-05
      • 1970-01-01
      • 2011-07-08
      • 2013-10-21
      • 2013-10-19
      • 2022-12-22
      相关资源
      最近更新 更多