我試圖創建一個程序,從文件中選擇一個隨機單詞,並根據單詞的長度輸出正確數量的空格。例如,如果這個詞很聰明,程序會輸出_ _ _ _ _。這是程序:錯誤:無法找到符號顯示Java初學者
import java.util.*;
import java.io.*;
public class Selection {
public static void main(String[] args) throws IOException {
String[] words = new String[100];
Scanner in = new Scanner(new FileReader("words.txt"));
for (int i = 0; i < 100; i++)
words[i] = in.next();
Random r = new Random();
int selected = r.nextInt[100];
String sWord = words[selected];
for (int j = 0; j < sWord.length(); j++)
System.out.printf("_");
in.close();
}
}
這是錯誤:
java:18: error: cannot find symbol
int selected = r.nextInt[100];
^
symbol: variable nextInt
location: variable r of type Random
1 error
瞭解什麼,我沒有正確地做任何幫助將不勝感激。
只需將您的代碼改成這樣:'選擇的int = r.nextInt(100);' – FoggyDay