我试图得到随机状态和大写字母的输出,但我的输出总是问“阿拉巴马州的首都是什么?”然后显示下一行的错误。我怎样才能正确地询问赎金州和资本呢?
public static void main (String args [])throws FileNotFoundException {
Scanner input= null;
Scanner file=null;
try
{ input= new Scanner(System.in);
file= new Scanner (new File ("capitals.txt"));
while (file.hasNext()){
String[] myString = file.next().split (",");
System.out.println(String.format ("What is the capital of %s?", myString[0]));
System.out.println(input.next().equals(myString[1]));
}
}
catch (FileNotFoundException fe){
}
finally {
file.close();
input.close();
}
}
答案 0 :(得分:0)
我的代码中没有任何随机性。这些是步骤(根据您目前的情况):
简化示例:
Random rnd = new Random();
String[] capitals = readFromFile();
int x = rnd.nextInt(capitals.length);
System.out.println(String.format ("What is the capital of %s?", capitals[x]));