我刚开始学习Java,所以如果这个问题有点基础,我很抱歉,我确信我的代码不是那么干净。我一直在尝试编写一个小测验程序,它将从txt文件(verblist.txt
)输入德语动词列表。文本文件的每一行包含五个字符串:德语动词(verbger
),英语翻译(verbeng
),praeteritum和perfekt共轭变形(verbprae
和verbperf
)以及它是否使用haben或sein作为辅助动词(H
或S
,存储为verbhaben
)。通过生成随机数并选择二维数组的“行”来选择动词集。然后GUI显示前两个变量,用户必须输入最后三个变量。如果最后三个匹配txt文件中的值,则用户将其设置为正确并转到另一个动词。
我正处于代码按照我想要的方式工作的地方 - 对于一个动词。我组织它的方式分为两类。一个VerbTable
将文本文件作为二维数组导入,另一个RunVerb
生成GUI并使用ActionListener比较用户对数组的输入。我现在无法弄清楚的是,在用户获得一个动词设置正确之后,我可以循环遍历整个动词集。
我正在考虑创建一个循环遍历文本文件中的行数(在代码中保存为height
)的for循环,每次生成一个新的随机数来选择不同的动词集(或者二维数组中的“row”。)基本上,我想得到一个循环来遍历整个VerbRun
类,并暂停ActionListener
,直到所有的动词集都被显示。
这是VerbTable class
,它生成数组和随机数以选择行:
package looptest;
import java.io.*;
import java.util.*;
public class VerbTable {
public int width;
public int height;
public int randnum;
public String verbger = new String("");
public String verbeng = new String("");
public String verbprae = new String("");
public String verbperf = new String("");
public String verbhaben = new String("");
public VerbTable() {
File file = new File("verblist.txt");
try {
/* For array height and width */
Scanner scanner1 = new Scanner(file).useDelimiter("\n");
int height = 0;
int width = 0;
while (scanner1.hasNextLine()) {
String line = scanner1.nextLine();
height++;
String[] line3 = line.split("\t");
width = line3.length;
}
System.out.println("Height: " + height);
System.out.println("Width: " + width);
this.width = width;
this.height = height;
/* Array height/width end */
/* random number generator */
int randnum1 = 1 + (int)(Math.random() * (height-1));
this.randnum = randnum1;
/* random number generator end */
Scanner scanner = new Scanner(file).useDelimiter("\n");
String verbtable[][];
verbtable = new String[width][height];
int j = 0;
while (scanner.hasNextLine()){
String verblist2 = scanner.next();
String[] verblist1 = verblist2.split("\t");
System.out.println(verblist2);
verbtable[0][j] = verblist1[0];
verbtable[1][j] = verblist1[1];
verbtable[2][j] = verblist1[2];
verbtable[3][j] = verblist1[3];
verbtable[4][j] = verblist1[4];
j++;
}
this.verbger = verbtable[0][randnum];
this.verbeng = verbtable[1][randnum];
this.verbprae = verbtable[2][randnum];
this.verbperf = verbtable[3][randnum];
this.verbhaben = verbtable[4][randnum];
}
catch (FileNotFoundException e){
e.printStackTrace();
}
}
public int getRand(){
return this.randnum;
}
public int getWidth(){
return this.width;
}
public int getHeight(){
return this.height;
}
public String getVerbger(){
return this.verbger;
}
public String getVerbeng(){
return this.verbeng;
}
public String getVerbprae(){
return this.verbprae;
}
public String getVerbperf(){
return this.verbperf;
}
public String getVerbhaben(){
return this.verbhaben;
}
public static void main(String[] args) throws IOException {
}
}
这是RunVerb类:
package looptest;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class RunVerb extends JFrame {
VerbTable dimensions = new VerbTable();
int width = dimensions.getWidth();
int height = dimensions.getHeight();
int randnum = dimensions.getRand();
String verbgerin = dimensions.getVerbger();
String verbengin = dimensions.getVerbeng();
String verbpraein = dimensions.getVerbprae();
String verbperfin = dimensions.getVerbperf();
String verbhabenin = dimensions.getVerbhaben();
String HabenSeinSelect = new String("");
public JTextField prae = new JTextField("",8);
public JTextField perf = new JTextField("",8);
public JLabel verbger = new JLabel(verbgerin);
public JLabel verbeng = new JLabel(verbengin);
public JRadioButton haben = new JRadioButton("Haben");
public JRadioButton sein = new JRadioButton("Sein");
public RunVerb() {
JButton enter = new JButton("Enter");
enter.addActionListener(new ConvertBtnListener());
prae.addActionListener(new ConvertBtnListener());
perf.addActionListener(new ConvertBtnListener());
JPanel content = new JPanel();
content.setLayout(new FlowLayout());
content.add(verbger);
verbger.setBorder(BorderFactory.createEmptyBorder(15, 20, 15, 20));
content.add(verbeng);
verbeng.setBorder(BorderFactory.createEmptyBorder(15, 20, 15, 40));
content.add(new JLabel("Praeteritum:"));
content.add(prae);
content.add(new JLabel("Perfekt:"));
content.add(perf);
content.add(haben);
haben.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 10));
haben.setSelected(true);
content.add(sein);
sein.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10));
ButtonGroup bg = new ButtonGroup();
bg.add(haben);
bg.add(sein);
content.add(enter);
setContentPane(content);
pack();
setTitle("Verben");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
class ConvertBtnListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String outprae = prae.getText();
int praenum = 0;
if (outprae.equals(verbpraein)){
praenum = 1;
}
String outperf = perf.getText();
int perfnum = 0;
if (outperf.equals(verbperfin)){
perfnum = 1;
}
boolean habenselect = haben.isSelected();
boolean seinselect = sein.isSelected();
if (habenselect == true){
HabenSeinSelect = "H";
}
else {
HabenSeinSelect = "S";
}
int habennum = 0;
if (HabenSeinSelect.equals(verbhabenin)) {
habennum = 1;
}
int numtot = praenum + perfnum + habennum;
if (numtot == 3){
System.out.println("Correct.");
}
else{
System.out.println("Incorrect.");
}
numtot = 0;
}
}
public static void main(String[] args) {
window.setVisible(true);
}
}
那么在显示所有行之前循环遍历整个verbtable
数组的最佳方法是什么?我应该创建一个for循环,如果是,它应该去哪里?我应该创建一个包含循环的新类并引用VerbRun
类吗?如果是这样,最好的方法是什么?
我希望这是有道理的!谢谢!
答案 0 :(得分:1)
要以随机顺序查看所有动词,您可能不希望每次都生成随机数,因为随机数可以重复。你必须创建一个动词的随机排列,一种方法是Collections.shuffle
请参阅http://download.oracle.com/javase/6/docs/api/java/util/Collections.html
此外,您不必创建新的RunVerb对象,而是创建一次,并使用setter来更改UI和Action Listeners的功能。所以伪代码将是
Collections.shuffle(verbsList);
for(verb : verbsList)
{
setLabel1(verb[0]);
setLabel2(verb[1]);...
}
答案 1 :(得分:0)
我会在getNextRandomVerb()
类中使用类似VerbTable
的方法来生成要显示的下一个动词。它将跟踪已经显示的动词(对于给定的用户会话)并确保下一个被选中的动词不是重复。您的RunVerb
类似乎更负责管理GUI,因此这不是定义如何显示下一个动词的地方。