有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

找不到符号错误Java

所以我创建了这个随机字符串生成器:

public class Test {
  public static void main(String[] args) {
  String strings = "abcdefghijklmnopqrstuvwxyz1234567890!@#$%&()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测试编译了它。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 = "abcdefghijklmnopqrstuvwxyz1234567890!@#$%&()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);
    }
  }
}

代码运行时没有错误,但不会打印任何内容。 我目前正在使用命令提示符编译它


共 (5) 个答案

  1. # 1 楼答案

    您仅在for循环内定义outputString。在室外是不可用的

    您可以直接输出字符:

    for (int i = 0; i < stringLength; i++) {
      Double test = new Double(Math.random());
      character = strings.charAt(test.intValue());
      System.out.print(character);
    }
    System.out.println();
    

    如果坚持在循环中连接字符串,请使用StringBuilder


    您将遇到的下一个问题:

    Double test = new Double(Math.random());
    strings.charAt(test.intValue());
    

    这将始终得到第一个字符。双精度将介于0(包括)和1(不包括)之间

    你需要create a random integer between 0 and the length of your alphabet

  2. # 2 楼答案

    我使用以下代码成功地使其工作:

    import java.util.Random;
    
    public class Test{
    
        public static void print(String str) {
          System.out.print(str);
        }
    
        public static int randomInt(int min, int max) {
            Random rand = new Random(System.currentTimeMillis());
            int randomNum = rand.nextInt((max-min)+1) - min;
            return randomNum;
        }
    
        public static void main(String[] args) throws InterruptedException {
            String strings = "abcdefghijklmnopqrstuvwxyz1234567890!@#$%&()ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            int len = strings.length() - 1;
            StringBuilder outputString = new StringBuilder(len);
            for (int i = 0; i < len; i++) {
                Double test = new Double(randomInt(0, len));
                Integer value = test.intValue();
                if (value <= len) {
                    //print(value.toString()); For testing purposes
                    outputString.append(strings.charAt(value));
                    Thread.sleep(1);
                }
                else {i--;}
            }
            print(outputString + "\n");
        }
    }
    
  3. # 3 楼答案

    outputString仅在for循环的块中定义,因此一旦退出它,它就不再存在。要确保代码正常工作,请在循环外部定义代码:

    String outputString = "";
    for (int i = 0; i < stringLength; i++) {
        Double test = new Double(Math.random());
        character = strings.charAt(test.intValue());
        outputString += character.toString();
    }
    System.out.println(outputString);
    
  4. # 4 楼答案

    这是因为您正在调用outputString。在你初始化它之前 试一试

    String outputString = "";
    outputString = outputString.concat(character.toString());`
    
  5. # 5 楼答案

    编译器正确地指出outputString未正确声明。outputString是在for循环中声明的,但您正试图在外部打印该值

    解决这个问题的一个快速方法是通过outputString = ""for循环之外声明outputString