【发布时间】:2016-04-14 15:41:42
【问题描述】:
char hangman[];
Scanner sc = new Scanner( System.in);
Random r = new Random();
File input = new File("ComputerText.txt").useDelimiter(",");
Scanner sc = new Scanner(input);
String words;
我想从 .txt 文件中读取一组单词,并让程序选择一个随机单词用于刽子手游戏。
下面的代码是用于当我们在代码中读取 .txt 文件时。 我们想使用三个不同的 .txt 文件,每个文件都有不同的类别,并让用户选择他们想要的词来自哪个类别。
//while(decision==1){word=computerWord;}
if ( decision == 1)
{
word=computerWord;
}
else if ( decision == 2)
{
word = countryWord;
}
else if (decision == 3)
{
word = fruitWord;
}
else
{
System.out.println("error, try again");
}
【问题讨论】:
-
什么不起作用?你遇到问题了吗?也许创建一个读取流来从文件中读取? BufferedReader 应该可以解决问题。
-
对不起 Bram,我使用 java 的时间不长,只知道基础知识。当我们这样写时,主程序正在读取单词: String[] word = {"program", "computer", "download", "database"};但我希望程序从文本文件中读取这些单词,并让文件使用随机类来选择一个单词
-
@sarahjane call sc.next();
-
Null Saint,您是指问题第二部分中的用户输入吗?就像 sc.next() 将允许用户从三个选项中选择一个?
-
@sarahjane 您已将扫描仪设置为从文件变量中读取,但为了从该文件中读取,请调用 sc.next()
标签: java file if-statement random