【发布时间】:2015-01-16 19:00:45
【问题描述】:
我正在尝试为我的在线课程创建一个猜谜游戏程序,其中有很多问题,您可以回答它们,但是我在程序中遇到了一些困难。 .
现在,我要做的是从“actionPerformed(ActionEvent event)”方法中读取文件,然后显示用户回答是否正确...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Scanner;
public class allQuestions implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(event.getActionCommand().equals("Answer M 100"))
{
try
{
File answersFile = new File("answers_file.txt");
Scanner sc = new Scanner(answersFile);
if(sc.hasNext("1000000"))
{
System.out.println("Your answer is correct!");
}
else
{
System.out.println("Your answer is wrong.");
}
}
catch(IOException e)
{
System.out.println("File cannot load.");
}
}
}
public static void math100()
{
JFrame m100Frame = new JFrame("100 Point Math Question");
m100Frame.setSize(350,350);
m100Frame.setLocationRelativeTo(null);
JPanel pane = new JPanel();
m100Frame.setContentPane(pane);
JLabel question = new JLabel("<html><p><div WIDTH = 320><center>Round 1,000,203 to the nearest thousands, and round 8.472 to the nearest hundredth.</p><p>Put answers in box below, and have the word 'and' between the two answers.</center></width></div></html>");
JTextArea answer = new JTextArea("Enter Answers Here", 10, 25);
JButton answerQuestion = new JButton("Answer M 100");
answerQuestion.addActionListener(new allQuestions());
pane.add(question);
pane.add(answer);
pane.add(answerQuestion);
m100Frame.setVisible(true);
m100Frame.toFront();
}
}
现在我不知道我做错了什么,但我试图做到这一点,以便当用户在框中输入他们的答案并按 Enter 键时,它会扫描文件以查看他们的答案是否正确,然后显示正确或错误...
希望这能解释一下。感谢您的帮助。
【问题讨论】:
-
IOException = 文件有问题。它存在吗?在哪个文件夹中?
-
你应该用
java标记问题,而不是javascript。 -
Theirry,我的文件夹中有它需要的文件,但我不断收到错误消息说它需要抛出 IOException,但它不允许我将它添加到 actionPerformed方法...
-
你是在编译,还是在线课程'bot'告诉你的?
-
它告诉我编译它。真的很难解释。就像我想创建一个教育猜谜游戏的应用程序一样,所以我正在制作一个游戏,您可以在其中单击特定问题的特定按钮,当您单击特定问题时,假设会显示问题和文本框。在文本框中,您应该回答问题,然后点击按钮回答问题。然后它会转到我创建的文件并搜索您输入的答案,然后显示答案是否正确。但它不适合我。
标签: java