我的Java代码在执行过程中停止

huangapple go评论75阅读模式
英文:

My Java code stops in the middle of execution

问题


public class passWord {
   public static void main(String[] args) {
      System.out.println("欢迎使用 passGen");
      System.out.println("请输入密码:");
      Scanner passWordScanner = new Scanner(System.in);
      String passWordString = passWordScanner.nextLine();
      System.out.println("正在检查密码强度...");
      passWordStrengthINT(passWordString);
   }

   // 其他方法的翻译与内容保持一致...
}

请注意,上述只是给出了主要方法的翻译示例。如果您需要其他方法的翻译,可以使用类似的方式进行翻译。另外,如果您遇到代码停止执行的问题,可以检查您的代码逻辑是否正确,是否存在无限循环或逻辑错误等问题。字符串的长度在Java中理论上没有固定的限制,但是如果您遇到了长度问题,可能是由于其他逻辑问题导致的。

英文:

My Java project while running stops in the main method even though I have given it accurate redirecting and all that stuff...My code and explanation is below.


public class passWord {
public static void main(String[] args) {
System.out.println("Welcome to passGen");
System.out.println("Please enter password here");
Scanner passWordScanner = new Scanner(System.in);
String passWordString = passWordScanner.nextLine();
System.out.println("Checking password strength :-)...");
passWordStrengthINT(passWordString);
}
public static void passWordStrengthINT(String passWordString) {
double passWordStrengthScore = 0;
if (passWordString.contains("1")) {
passWordStrengthScore = 0.5 * 2;
}
if (passWordString.contains("2")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("3")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("4")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("5")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("6")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("7")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("8")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("9")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("0")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
passWordStrengthCharS(passWordString, passWordStrengthScore);
}
public static void passWordStrengthCharS(String passWordString, double passWordStrengthScore) {
if (passWordString.contains("a")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("b")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("c")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("d")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("e")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("f")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("g")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("h")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("i")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("j")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("k")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("l")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("m")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("n")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("o")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("p")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("q")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("r")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("s")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("t")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("u")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("v")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("w")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("x")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("y")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("z")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
passWordStrengthL(passWordString, passWordStrengthScore);
}
public static void passWordStrengthL(String passWordString, double passWordStrengthScore) {
if (passWordString.contains("A")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("B")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("C")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("D")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("E")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("F")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("G")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("H")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("I")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("J")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("K")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("L")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("M")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("N")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("O")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("P")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("Q")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("R")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("S")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("T")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("U")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("V")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("W")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("X")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("Y")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("Z")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
passWordStrengthSym(passWordString, passWordStrengthScore);
}
public static void passWordStrengthSym(String passWordString, double passWordStrengthScore) {
if (passWordString.contains("!")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("@")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("#")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("$")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("%")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("^")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("&")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("*")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("(")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains(")")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("_")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("-")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("<")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains(">")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("/")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
if (passWordString.contains("?")) {
passWordStrengthScore = passWordStrengthScore + 0.5 * 2;
}
passWordLength(passWordString, passWordStrengthScore);
}
public static void passWordLength(String passWordString, double passWordStrengthScore) {
int passWordLength = passWordString.length();
if (passWordLength > 3 && passWordLength < 5) {
passWordStrengthScore = passWordStrengthScore + 0.1 * 2;
}
if (passWordLength < 3 && passWordLength > 1) {
passWordStrengthScore = passWordStrengthScore - 0.1 * 2;
}
if (passWordLength < 10 && passWordLength > 5) {
passWordStrengthScore =+ 0.1 * 2;
}
if (passWordLength > 10)  {
passWordStrengthScore =+ 10 * 2;
}
ScoreCheck(passWordStrengthScore, passWordString);
}
public static void ScoreCheck(double passWordStrengthScore, String passWordString) {
if (passWordStrengthScore > 10) {
decreaseScore(passWordStrengthScore, passWordString);
} else {
System.out.println("Your Password Strength Score is " + passWordStrengthScore);
wordCompliments(passWordStrengthScore, passWordString);
}
}
public static void wordCompliments(double passWordStrengthScore, String passWordString) {
if (passWordStrengthScore < 3) {
System.out.println(" Your PassWord " + passWordString + " is WEAK :-(");
}
if (passWordStrengthScore > 3 && passWordStrengthScore < 5) {
System.out.println(" Your PassWord " + passWordString + " is OKAY :-|");
}
if (passWordStrengthScore > 5 && passWordStrengthScore < 10) {
System.out.println(" Your PassWord " + passWordString + " is Good :-|");
}
if (passWordStrengthScore == 10) {
System.out.println(" Your PassWord " + passWordString + " is Excellent and Strong :-D");
}
}
public static void decreaseScore(double passWordStrengthScore, String passWordString) {
for (int i = 0; i < 10; i++) {
if (passWordStrengthScore > 10) {
passWordStrengthScore--;
} else {
ScoreCheck(passWordStrengthScore, passWordString);
break;
}
}
}
}

Pretty big, huh 😎.

I tried changing the order of methods and tried lightening the code

I am working on a project in which user gives me their password and I check its strength. My code stops right after this line of code System.out.println("Checking password strength :-)...");

But I have noticed whenever I input a smaller value my code gets completed. I don't know why...Is there a limit of characters for the string?

Note: I am new to Java, so there is probably a lot to learn, I took this as a personal project 我的Java代码在执行过程中停止

答案1

得分: 0

不过,等一下。你需要确保这些语句是分开的。或者使用 else-if,并按字符对字母进行排序。

英文:

Nah, wait. You need to make sure the statements are separate. or do else-if's and sort the letters by character.

答案2

得分: 0

所以,正在发生的情况是,假设输入为"abasdak$53lksm",密码为passWordStrengthScore为20.0,现在在这段代码的部分:

public static void decreaseScore(double passWordStrengthScore, String passWordString) {
  for (int i = 0; i < 10; i++) {
     if (passWordStrengthScore > 10) {
        passWordStrengthScore--;
     } else {
        ScoreCheck(passWordStrengthScore, passWordString);
        break;
     }
  }
}

当passWordStrengthScore变为10时,在下一次迭代中,i变为10,因此循环失败,您的else块永远不会执行。因此,您回退到此代码:

decreaseScore(passWordStrengthScore, passWordString);

然后您只是返回到main函数(沿着您来时的路径),而不打印任何内容。您只需将for循环的上限从10更改为11。那应该就可以了。

英文:

So, what is happening is that, say for input "abasdak$53lksm" password your passWordStrengthScore is coming 20.0, now in this part of code:

public static void decreaseScore(double passWordStrengthScore, String passWordString) {
for (int i = 0; i &lt; 10; i++) {
if (passWordStrengthScore &gt; 10) {
passWordStrengthScore--;
} else {
ScoreCheck(passWordStrengthScore, passWordString);
break;
}
}
}

When passWordStrengthScore becomes 10, i becomes 10 in the next iteration, thus the loop fails and your else block is never executed. Hence, you fall back to this code:

decreaseScore(passWordStrengthScore, passWordString);

and then you just return to main(along the path you came) without printing anything. You can just change the upper limit of for loop from 10 to 11. That should do.

答案3

得分: 0

尝试将这个概念应用于所有这些条件语句的地方。使用循环来检查字符的ASCII码。例如,你现在正在检查小写字母,因此小写字母的ASCII码范围在97到122之间(https://www.ascii-code.com/)。每次循环发现在这个范围内的字符,就会增加字母计数。接下来,你可以为你的强度点数分配任何值/字母,以计算密码的总强度值。

String pw = "abcdefg";
int count = 0;
double point = 1.5;

for(int i = 0; i < pw.length(); i++){
    if((int)pw.charAt(i) >= 97 && (int)pw.charAt(i) <= 122){
        count++;
    }
}

System.out.println("密码强度: " + count * point);
英文:

Try to apply this concept instead of all those ifs. Using a loop to check the ASCII code of the character. For example you are now checking the lower case alphabets, hence the ASCII code for lower case alphabet is in range 97 to 122 (https://www.ascii-code.com/). Each time the loop found something between the range it adds to the alphabet count. Next you can assign whatever value to your strength points / letter can calculate the total strength value of the password.

String pw = &quot;abcdefg&quot;;
int count = 0;
double point = 1.5;
for(int i = 0; i &lt; pw.length(); i++){
if((int)pw.charAt(i) &gt;= 97 &amp;&amp; (int)pw.charAt(i) &lt;= 122){
count++;
}
}
System.out.println(&quot;Password point: &quot; + count*point);

huangapple
  • 本文由 发表于 2020年10月13日 12:09:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64328355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定