【发布时间】:2021-07-01 01:14:44
【问题描述】:
所以代码工作正常,但是破解 5 个字母密码的尝试次数不正确。我试过修复东西,但它总是给我 3 位数。尝试的次数应该更高。 这是我的代码:
import java.util.Scanner;
import java.util.Random;
class Main {
public static void main(String[] args) {
// Letters for the random generated password
// Variables
String letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
Random order = new Random();
int PASSWORD = letters.length();
// While statement to allow user to keep generating passwords
while (true) {
String password = "";
Scanner input = new Scanner(System.in);
// Print/menu
System.out.println("Press 1 to generate a random password");
// Takes user input
int UserOption = input.nextInt();
// If user input equals 1
if (UserOption == 1) {
// Generate a 5-character passwords from the letters in the String
for (int i = 0; i < 5; i++) {
password = password + letters.charAt(order.nextInt(PASSWORD));
}
System.out.println(password);
cracking(5, password, letters, 0, "");
}
// If user input is anything except 1
else {
// Print error
System.out.println("Error");
}
}
}
//Method for cracking password
private static int cracking(int length, String password, String characters, int tries, String tryPass) {
System.out.println(length);
if (length == 0) {
System.out.println("It took " + tries + " tries to crack the password");
return 0;
}
for (int i = 0; i < characters.length(); i++) {
if (password.charAt(length-1) == characters.charAt(i)) {
tryPass = tryPass + characters.charAt(i);
break;
}
tries++;
}
cracking((length-1), password, characters, tries, tryPass);
return 0;
}
}
【问题讨论】: