gpt4 book ai didi

Java pass 检查不起作用,条件有效

转载 作者:行者123 更新时间:2023-11-30 06:27:42 25 4
gpt4 key购买 nike

import java.util.Scanner;

public class p1_l2_adrian_costin {

/*read string from keyboard*/
public static String read() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your password XXXXX-XXXXX-XXXXX-XXXXX \n");
String password = keyboard.nextLine();
return password;
}

/*check for number of words (4) and letters (5 per word), if ok pass true*/

public static boolean splitAndCheckLength(String s) {
String str[] = s.split("-");
int counter = 0;
for (int i = 0; i < str.length; i++)
if (str[i].length() == 5)
counter++;
if (counter == 4)
return true;
return false;
}

/*check for number of words compared to numbers, if there are more numbers than words pass true
*/

public static boolean countNumbersAndLetters(String s) {
String str[] = s.split("-");
int counterL = 0, counterN = 0;
char[] c;
for (int i = 0; i < str.length; i++) {
c = str[i].toCharArray();
for (int j = 0; j < c.length; j++) {
if ((c[j] > 64 && c[j] < 91) || (c[j] > 96 && c[j] < 123))
counterL++;
if (c[j] > 47 && c[j] < 58)
counterN++;
}
}
if (counterN > counterL && counterL != 0) return true;
return false;

}

public static void main(String[] args) {
if (countNumbersAndLetters(read()) && splitAndCheckLength(read()))
System.out.println("Good password");
System.out.println("Bad password");
}
}

问题是在评估每个表达式时,它们都返回 true,但是当将它们都放入 if 并尝试从主打印消息时,一切都会停止工作,并且只是再次开始输入您的密码

最佳答案

目前您调用了两次 read(),因此在调用 countNumbersAndLetters() 后,您会要求再次输入密码。

您只想获取用户输入一次:

public static void main(String[] args) {
String input = read();
if(countNumbersAndLetters(input) && splitAndCheckLength(input))
System.out.println("Good password");
else
System.out.println("Bad password");
}

关于Java pass 检查不起作用,条件有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46785188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com