所以我创建这个随机字符串发生器:找不到符号错误的Java
public class Test {
public static void main(String[] args) {
String strings = "[email protected]#$%&()ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random generator = new Random(System.currentTimeMillis());
int stringLength = strings.length()-1;
Character character;
for (int i = 0; i < stringLength; i++) {
Double test = new Double(Math.random());
character = strings.charAt(test.intValue());
String outputString = outputString.concat(character.toString());
}
System.out.println(outputString);
}
}
我去一个编译它使用javac Test.java,和它给我的错误outputString可能没有被初始化为线14和16.因此,我将字符串关键字添加到第14行,现在它告诉我 cannot find symbol: variable outputString
这是为什么?
编辑:
好了,所以我接受了建议,这是目前代码:
public class Test {
public static int randomInt(int min, int max, long seed) {
Random rand = new Random(seed);
int randomNum = rand.nextInt((max-min)+1) - min;
return randomNum;
}
public static void main(String[] args) {
String strings = "[email protected]#$%&()ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int stringLength = strings.length()-1;
Character character;
for (int i = 0; i < stringLength; i++) {
Double test = new Double(randomInt(0, stringLength, System.currentTimeMillis()));
character = strings.charAt(test.intValue());
System.out.print(character);
}
}
}
代码运行没有错误,但它不显示任何信息。 我目前使用命令提示编译它。
您的代码只有14行。第16行如何出现错误? – Barmar 2014-12-11 01:54:32
我在'public class test'之前有一个导入行。 – 2014-12-11 02:10:07
如果你没有在你的问题中引用行号会更好。即使它们是正确的,你是否真的希望我们能够计数? – Barmar 2014-12-11 02:11:17