String 如何清除for循环中的变量
这个程序将生成100个随机的4个字母的数字,它首先生成4个字母,然后再生成4个字母,并将它们添加到函数中。我猜是因为我使用了.append。我试着输入random=“”;在循环的顶部和底部,但它只是清除了变量。有没有其他方法可以做到这一点,或者有什么我可以添加到程序中String 如何清除for循环中的变量,string,loops,append,String,Loops,Append,这个程序将生成100个随机的4个字母的数字,它首先生成4个字母,然后再生成4个字母,并将它们添加到函数中。我猜是因为我使用了.append。我试着输入random=“”;在循环的顶部和底部,但它只是清除了变量。有没有其他方法可以做到这一点,或者有什么我可以添加到程序中 Random input = new Random(); StringBuilder in = new StringBuilder(); String alphabet = "abcdefghijklom
Random input = new Random();
StringBuilder in = new StringBuilder();
String alphabet = "abcdefghijklomnpqrstuvwxyz";
char[] letterPool;
letterPool = alphabet.toCharArray();
for(int y = 0; y < 101; y++){
for(int x = 0; x<4; x++){
in.append(alphabet.charAt(input.nextInt(alphabet.length())));
}
String random=in.toString();
System.out.println(random);
System.out.println("space");
}
Random input=new Random();
StringBuilder in=新的StringBuilder();
字符串字母表=“abcdefghijklomnpqrstuvxyz”;
字符[]字母池;
letterPool=alphabet.toCharArray();
对于(int y=0;y<101;y++){
for(int x=0;x每次外部for循环完成时,内部for循环将自行重置。Random input=new Random();
Random input = new Random();
String alphabet = "abcdefghijklomnpqrstuvwxyz";
for (int y = 0; y < 100; y++) {
StringBuilder in = new StringBuilder();
for (int x = 0; x < 4; x++) {
in.append(alphabet.charAt(input.nextInt(alphabet.length())));
}
String random = in.toString();
System.out.println(random);
}
字符串字母表=“abcdefghijklomnpqrstuvxyz”;
对于(int y=0;y<100;y++){
StringBuilder in=新的StringBuilder();
对于(int x=0;x<4;x++){
in.append(alphabet.charAt(input.nextInt(alphabet.length()));
}
字符串random=in.toString();
系统输出打印项次(随机);
}
Huh???谢谢!为什么在for循环之间移动StringBuilder可以解决这个问题?这样,在每次外部for
循环的迭代中,StringBuilder
将被重新初始化。