- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
此方法应该将下一个问题加载到 GUI 中。
public void readNextEntry() {
list = new ArrayList<String>();
scanner = new Scanner(linesFromFile.get(currentQuestion)).useDelimiter(",|\n");
for (int i = 0; i < 8; i++) {
list.add(scanner.next());
}
}
它应该加载的文本文件格式如下:
001,what color is the sky,it is blue because of precipitation,green,blue,yellow,red,a,
002,What color are my eyes,they are blue if you look,blue,brown,green,black,a,
003,What color is the water,it is blue because of water,green,blue,red,yellow,b,
为什么不起作用?它应该加载类别(001
)、问题(天空是什么颜色?
)以及它的选择(绿色、蓝色、黄色,红色
)。我的正则表达式关闭了吗?
编辑:一些上下文,这是完整的代码。到目前为止,一直在尝试此线程的建议,但没有成功。
package TestAssignment;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.border.Border;
public class TestGui extends JFrame implements ActionListener {
private int counter = 0;
private int totalCount = 0;
private JTextArea question = new JTextArea();
private JTextArea recomendedAnswer = new JTextArea();
private JTextArea category = new JTextArea();
private ArrayList<String> list;
private ArrayList<String> linesFromFile;
private int currentQuestion = 0;
private ButtonGroup group = new ButtonGroup();
private ArrayList<JRadioButton> theButtons;
private JLabel response = new JLabel("");
private JButton answer = new JButton("Answer Question");
private Border border = BorderFactory.createLineBorder(Color.BLACK, 1);
private Scanner scanner;
private String fileName;
public void readNextEntry() {
list = new ArrayList<String>();
scanner = new Scanner(linesFromFile.get(currentQuestion)).useDelimiter(",|\n");
for (int i = 0; i < 8; i++) {
list.add(scanner.next());
}
}
public void readFile() {
linesFromFile = new ArrayList<String>();
list = new ArrayList<String>();
try {
scanner = new Scanner(new File(fileName));
while (scanner.hasNextLine()) {
linesFromFile.add(scanner.nextLine());
}
Collections.shuffle(linesFromFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public TestGui(String in) {
setLayout(new GridLayout(11, 25));
this.fileName = in;
add(new JLabel("Category : "));
JScrollPane scrollPane1 = new JScrollPane(category);
category.setLineWrap(true);
category.setBorder(border);
add(scrollPane1);
category.setEditable(false);
//int randomNumber = (int) (Math.random() * 10) + 1;
//String line = TestGui.readFileByLine("Questions.txt", randomNumber);
add(new JLabel("Question: "));
JScrollPane scrollPane2 = new JScrollPane(question);
question.setLineWrap(true);
question.setBorder(border);
add(scrollPane2);
question.setEditable(false);
readFile();
readNextEntry();
add(new JLabel("Please Select the correct Answer"));
theButtons = new ArrayList<JRadioButton>();
for (int i = 0; i < 4; i++) {
JRadioButton temp = new JRadioButton();
theButtons.add(temp);
add(temp);
group.add(temp);
}
refreshButtonLabels();
add(response);
category.setText(list.get(0));
question.setText(list.get(1));
add(answer);
answer.addActionListener(this);
}
public void refreshButtonLabels() {
for (int i = 0; i < 4; i++) {
theButtons.get(i).setText(list.get(i + 3));
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == answer) {
for (int i = 0; i < theButtons.size(); i++) {
JRadioButton button = theButtons.get(i);
if (button.isSelected() && list.get(7).equals("a")) {
counter++;
readNextEntry();
}
else if (button.isSelected() && list.get(7).equals("b")){
counter++;
readNextEntry();
}
else if (button.isSelected() && list.get(7).equals("c")){
counter++;
readNextEntry();
}
else if (button.isSelected() && list.get(7).equals("d")){
counter++;
readNextEntry();
}
}
}
else {
readNextEntry();
{
}
}
package TestAssignment;
import javax.swing.JFrame;
public class Test {
public static void main(String[] args) {
TestGui test = new TestGui();
test.setVisible(true);
test.setSize(600, 600);
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
最佳答案
您会发现用逗号分隔字符串要容易得多:
String line = linesFromFile.get(currentQuestion);
String[] list = line.split(",\\s*");
扫描仪只是妨碍你。但是,当您确实使用扫描程序时,切勿在未先调用关联的 hasNextXXX()
方法的情况下调用其 nextXXX()
方法之一。例如:
while (scanner.hasNext())
{
list.add(scanner.next());
}
如果使用正确,Scanner 永远不会抛出 NoSuchElementException。
关于java - 为什么我的代码不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13537216/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!