从Java中的嵌套for循环获取密码/字符列表

从Java中的嵌套for循环获取密码/字符列表,java,for-loop,nested,Java,For Loop,Nested,我了解嵌套for循环是如何工作的,但我还不知道如何在Java中实现它们。在我的代码中,一定有一个放错地方的花括号,或者一个命令不合适。我已经尝试了几个小时使用不同的配置,但没有结果 我已经研究了其他示例,包括其他编程语言中的一些示例 我试图在文本框中获得的输出如下所示 ekdk15hY7S 8fk4Wma7Ht 5KR278NHS 等等 但我得到的只是文本框中的一行,而不是我想要的列表。我通过GUI告诉程序我需要多少密码以及每个密码有多少个字符/数字 当我注释掉第一个for循环并添加或删除一个花

我了解嵌套for循环是如何工作的,但我还不知道如何在Java中实现它们。在我的代码中,一定有一个放错地方的花括号,或者一个命令不合适。我已经尝试了几个小时使用不同的配置,但没有结果

我已经研究了其他示例,包括其他编程语言中的一些示例

我试图在文本框中获得的输出如下所示

ekdk15hY7S

8fk4Wma7Ht

5KR278NHS

等等

但我得到的只是文本框中的一行,而不是我想要的列表。我通过GUI告诉程序我需要多少密码以及每个密码有多少个字符/数字

当我注释掉第一个for循环并添加或删除一个花括号时,代码可以很好地为我提供一个密码

有人能看出我做错了什么吗?代码如下:

//  numb is how many characters per password.
String numbSTR = spinner1.getValue().toString();
int numb = Integer.parseInt(numbSTR);               

//count is how many passwords do you want.
String countSTR = spinner2.getValue().toString();
int count = Integer.parseInt(countSTR);


//count is how many passwords do you want.
for (int x = 0; x < count; x++ ) {
    String buildPW = "";
    //numb is how many characters per password.               
    for (int y = 0; y < numb; y++ ) {
        Random position = new SecureRandom();
        String digits =  "0123456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";                                                                         
        int index = (int)     (position.nextDouble()*digits.length());
        buildPW += digits.substring(index, index+1);      
    }       

    TextArea1.setText(buildPW + crlf); // put each new password in the textbox.
}       

将settext和buildPW移到循环之外:

// Moved
String buildPW = "";
//count is how many passwords do you want.
for (int x = 0; x < count; x++ ) {
    //numb is how many characters per password.               
    for (int y = 0; y < numb; y++ ) {
        Random position = new SecureRandom();
        String digits =  "0123456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";                                                                         
        int index = (int)     (position.nextDouble()*digits.length());
        buildPW += digits.substring(index, index+1) + crlf;      
    }
} 
// Moved
TextArea1.setText(buildPW); // put each new password in the textbox.

你的代码运行得很好。以下是大致相同的版本,它可以工作:

public class HelloWorld {
  public static void main(String[] args) {

     int count = 10;
     int numb = 8;
     for (int x = 0; x < count; x++ ) {
     String buildPW = "";
     for (int y = 0; y < numb; y++ ) {

        String digits =      "0123456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";                                                                         
        int index = (int)     (Math.random()*digits.length());
        buildPW += digits.substring(index, index+1);
      }     

   System.out.println(buildPW);
   }  


  }
}

它应该会起作用


SetText,一次替换textArea中的整个文本。在您的情况下,您可以将textArea的内容设置为最新的密码。另一方面,appendText将给定文本追加到textArea中文本的末尾。与上面示例中的System.out.println代码类似。

这不起作用,因为每个密码末尾没有crlf。3个密码,每个8个字符,看起来可能像bclzrhab1jtkyngqy1ntombt,只需将crlf弹出到此行:buildPW+=digits.substringindex,index+1+crlf;这不起作用。我相信这是我之前尝试过的配置之一。谢谢你的回复。我明白了真相!如您所说,将其更改为.appendText,这样做是有效的。这就像是向已经存在的文本文件添加更多数据。非常感谢。@Krang很乐意帮忙=]
 TextArea1.setText(buildPW + crlf);
 TextArea1.appendText(buildPW + crlf);