【问题标题】:My while loop inside my for loop is making the for loop infinite. Java我的 for 循环中的 while 循环使 for 循环无限。爪哇
【发布时间】:2018-05-24 12:06:16
【问题描述】:
import java.util.Scanner;
import java.util.Random;

public class SecretPasscodes {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    Random rand = new Random();

    System.out.println("#########################|Password Generator|#######################");
    System.out.println("#  [1] Lowercase Letters                                           #");
    System.out.println("#  [2] Uppercase and Lowercase Letters                             #");
    System.out.println("#  [3] Letters and numbers                                         #");
    System.out.println("#  [4] Lowercase and Uppercase letters, Numbers , and symbols      #");
    System.out.println("#  [5] Quit                                                        #");
    System.out.println("####################################################################");
    System.out.println("Enter your selection(1-5)");
    // Variables
    int choice = in.nextInt();

    System.out.println("Password Length(1-14");
    int passLength = in.nextInt();

    // Lowercase
    if (choice == 1) {
        for (int counter = 0; counter < passLength; counter++) {
            int lowerLetter = rand.nextInt((122 - 97) + 1) + 97;
            System.out.print((char) lowerLetter);
        }

        // Uppercase + lowercase
    } else if (choice == 2) {
        for (int counter = 0; counter < passLength; counter++) {
            int ascii = rand.nextInt(255);
            while ((ascii >= 65 && ascii <= 90) || (ascii >= 97 && ascii <= 122)) {
                System.out.print((char) ascii);

            }
        }
    }
}
}

如果删除了 while 循环,for 循环将不会是无限的。一旦我添加了 while 循环,for 循环就变成了无限的。我被这个问题困扰了一段时间,我的 apcs 老师没有回应。

谢谢!

【问题讨论】:

  • 我不认为你想要一个while循环。我猜你想重复 rand.nextInt 直到它给你一个范围内的值?在这种情况下,int ascii = rand.nextInt(255); 需要在循环内,并且您需要 if 它在可接受的范围内,如果是,则打印并中断。更好的是,为什么不尝试构建一个可打印的值呢?使用两个 rands,一个选择一个字母,一个选择一个大小写,或者 nextInt(52) 并将其拆分为大写和小写,还是其他?
  • 另一种(可能违背精神)做这种事情的方法是为每个集合定义一个字符串,例如"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" 然后从该字符串中随机选择一个字符。那你就完全不用担心ASCII码了。
  • 您正在生成随机整数,并且您可能总是得到这些范围之间的数字,
  • 从技术上讲,for 循环不会变成无限的。如果你第一次得到一个错误的整数,它永远不会超过一次迭代。

标签: java loops while-loop infinite-loop


【解决方案1】:

这对您来说失败的原因是您在进入 while 循环之前分配了 ascii。此值在 while 循环中不会更改,因此它将始终包含您在进入 while 循环之前设置的值。您要么需要更改 while 条件,要么需要修改 while 块内的 ascii,使其不再满足指定的条件。你也可以在你的 writeline 之后添加 break。 (让它像 if 语句一样工作)。您的代码可以概括为:

While(1)
{
    print(1)
}

【讨论】:

    【解决方案2】:

    我想你想做这样的事情:

    else if (choice == 2) {
        for (int counter = 0; counter < passLength; counter++) {
            int ascii = rand.nextInt(255);
            while (true) {
                if ((ascii >= 65 && ascii <= 90) || (ascii >= 97 && ascii <= 122)) {
                    System.out.print((char) ascii);
                    break;
                } else {
                    ascii = rand.nextInt(255);
                }
            }
        }
         System.out.println();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 2023-01-22
      • 2018-11-05
      • 2019-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多