2014-12-11 82 views
0

所以我創建這個隨機字符串發生器:找不到符號錯誤的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); 
    } 
    } 
} 

代碼運行沒有錯誤,但它不顯示任何信息。 我目前使用命令提示編譯它。

+0

您的代碼只有14行。第16行如何出現錯誤? – Barmar 2014-12-11 01:54:32

+0

我在'public class test'之前有一個導入行。 – 2014-12-11 02:10:07

+0

如果你沒有在你的問題中引用行號會更好。即使它們是正確的,你是否真的希望我們能夠計數? – Barmar 2014-12-11 02:11:17

回答

0

我設法得到它使用以下代碼工作:

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 = "[email protected]#$%&()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"); 
    } 
} 
2

您只在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

+0

當我在循環外部定義了outputString時,它給了我兩個錯誤,表示outputString可能未被初始化 – 2014-12-11 02:11:19

+0

您想將其初始化爲空字符串('「」'); – Thilo 2014-12-11 02:25:54

0

編譯器正確地告知outputString未正確聲明。 outputStringfor循環內聲明,但您試圖在外部打印該值。

一個快速的方法來解決,這將是由具有outputString = ""申報outputStringfor環路之外;

+0

感謝您對其進行了適當的更改。 – kuriouscoder 2014-12-11 01:57:27

0

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); 
0

這是因爲你在呼喚outputString.concat你intialized之前 嘗試

String outputString = ""; 
outputString = outputString.concat(character.toString());`