我正在尝试创建一个程序,该程序从文件中选择一个随机单词并根据单词的长度输出正确的空格数。例如,如果单词是 smart,则程序输出 _ _ _ _ _。这是程序:
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
任何有助于理解我没有正确做的事情将不胜感激。