Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用带有字符和整数的嵌套for循环?_Java - Fatal编程技术网

Java 如何使用带有字符和整数的嵌套for循环?

Java 如何使用带有字符和整数的嵌套for循环?,java,Java,我正在创建一个表单,将一本新书添加到图书馆系统中,用户在其中提供有关该书的一些详细信息,如“标题”或“类别” 在“作者”字段中,考虑到该书可以由多个作者编写,系统会询问用户作者的数量 如果用户输入数字“n”,则系统通过“Author”字段迭代“n”次,并将输入收集到字符串ArrayList中 我的目标是在字段描述后添加一个字母,以区分一个输入和另一个输入,因此如果有“n”个作者,那么系统将要求“作者a”,然后是“作者B”,依此类推 以下是我的代码片段: //The system ask

我正在创建一个表单,将一本新书添加到图书馆系统中,用户在其中提供有关该书的一些详细信息,如“标题”或“类别”

  • 在“作者”字段中,考虑到该书可以由多个作者编写,系统会询问用户作者的数量

  • 如果用户输入数字“n”,则系统通过“Author”字段迭代“n”次,并将输入收集到字符串ArrayList中

  • 我的目标是在字段描述后添加一个字母,以区分一个输入和另一个输入,因此如果有“n”个作者,那么系统将要求“作者a”,然后是“作者B”,依此类推

以下是我的代码片段:

//The system asks for the String "numberOfAuthors".
for (int i = 0; i < Integer.parseInt(numberOfAuthors); i++) {
    //I limited the number of authors to maximum 3, that's why it iterates until "C".
    for (char abc = 'A'; abc < 'D'; abc++) {

        System.out.print("\nAuthor " + abc + " :" + "\n> ");
        String author = userInput.getString();

        while (!authorPattern.matcher(author).matches()) {
            System.out.print("\n\"" + author + "\"" + authorWarning);
            System.out.print("\nAuthor " + abc + " :" + "\n> ");
            author = userInput.getString();
        }

        authors.add(author.toLowerCase());
        setAuthor(String.valueOf(authors));
    }
}
  • 我确实尝试打破嵌套循环,因为我知道它是(不总是) 被认为是坏习惯,但不起作用

  • 我还尝试将char改为String,这样我就可以比较“abc”和“I”,但它也不起作用

  • 如果还有别的办法,我洗耳恭听

  • 提前谢谢


  • 遵循@Polygenme的建议,我得到了以下建议:

             //I got rid of the second loop, now it works just fine.
              char[] letters = {'A', 'B', 'C'};
    
                for (int i = 0; i < Integer.parseInt(numberOfAuthors); i++) {
                    System.out.print("\nAuthor " + letters[i] + " :" + "\n> ");
                    String author = userInput.getString();
    
                    while (!authorPattern.matcher(author).matches()) {
                        System.out.print("\n\"" + author + "\"" + authorWarning);
                        System.out.print("\nAuthor " + letters[i] + " :" + "\n> ");
                        author = userInput.getString();
                    }
    
                    authors.add(author.toLowerCase());
                    setAuthor(String.valueOf(authors));
                }
            }
    

    谢谢

    不要嵌套循环!使用单循环为什么您认为需要内循环?这应该达到什么目的?为什么不直接打印
    “Author”+i
    @polygenome我做了,效果很好。我只是想得到“Author A”而不是“Author 0”。@HovercraftFullOfEels我知道这是一种不好的做法,但如果有其他方法实现它,我想学习然后在打印中:
    “Author”+chars[i]
             //I got rid of the second loop, now it works just fine.
              char[] letters = {'A', 'B', 'C'};
    
                for (int i = 0; i < Integer.parseInt(numberOfAuthors); i++) {
                    System.out.print("\nAuthor " + letters[i] + " :" + "\n> ");
                    String author = userInput.getString();
    
                    while (!authorPattern.matcher(author).matches()) {
                        System.out.print("\n\"" + author + "\"" + authorWarning);
                        System.out.print("\nAuthor " + letters[i] + " :" + "\n> ");
                        author = userInput.getString();
                    }
    
                    authors.add(author.toLowerCase());
                    setAuthor(String.valueOf(authors));
                }
            }
    
    Number of Authors:
    > 3
    
    Author A :
    > john doe
    
    Author B :
    > maria young
    
    Author C :
    > sinead parks
    
    Number of Categories:
    >