1
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に両方を置くとmainからのメッセージを表示しようとすると、あなたのパスワードを入力するJavaパスチェックが機能しません。条件が有効です。
あなたはおそらく '読んで()'一度なし、メソッドに値を渡す必要がありますか? – AxelH
一般的に、チェックが合格するか、合格しなかった場合、「条件が有効です」と表示されますが、一部のデバッグではJavaコードが要求したとおりに実行されました。 –
新しい「_」パスワードXXXXX-XXXXX-XXXXX-XXXXX_ "があなたのアプリが停止した理由を教えてくれるはずです。 – AxelH