0
我的任務是要我們用圖像問一個問題(使用我在java項目中導入的圖像文件)。我如何從我的方法中選擇一個隨機圖像文件?例如,在我的第二行代碼中,我有一個顯示圖片的圖像文件,該文件來自下面的「states_capitals」方法。我該如何改變它,只顯示我列出的隨機文件圖像?如何從方法中選取隨機圖像來顯示?
public void start(Stage primaryStage){
ImageView us = new ImageView(new Image("file:///Users/flag.png"));
Scene scene = new Scene (pane2, 600, 300);
primaryStage.setScene(scene);
primaryStage.show();
submit_button.setOnAction(e -> {
correct_wrong();
});
}
private void correct_wrong() {
String index = answer_box.getText();
Map<String, String> mapStateCapitals = new HashMap<>(50);
for (String[] stateCapital : states_capitals) {
mapStateCapitals.put(stateCapital[0], stateCapital[1]);
}
mapStateCapitals.forEach((state, capital) -> {
if (index.equalsIgnoreCase(capital)) {
answer_result.appendText("Correct!");
} else {
answer_result.appendText("WRONG - The correct answer is " + capital);
}
});
}
private static String[][] states_capitals = {
{"file:///Users/flag.png", "Siri"},
{"file:///Users/flag.png2.png", "Juju"},
{"file:///Users/flag.png3.png", "Cambodia"},
};
public static void main(String[] args) {
launch(args);
}