我试图用一款新游戏制作节目'按下时按钮询问用户一个新问题。这是一个关于州首府的问题,我希望每个新游戏只改变问题中的州名。这可能是一种愚蠢的方式,但我只是在尝试并尝试更多地了解java。
我遇到的问题是它只能运行一次。我按下新游戏,第一次就改变了,然后它什么也没做。我尝试过使用while和for循环的各种组合,但没有运气。当我尝试在for循环的某些组合中使用counter ++时,程序甚至会崩溃!所以,是的,我非常困难,所以任何帮助都会非常感谢。
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args){
Gui a = new Gui();
List<String> stateList = new ArrayList<String>();
List<String> capsList = new ArrayList<String>();
for(String x: StateData.states)
stateList.add(x);
for(String z: StateData.caps)
capsList.add(z);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Gui extends JFrame {
private static final long serialVersionUID = 1L;
JLabel instructions = new JLabel("What is the capital of Alabama?");
JLabel aLabel = new JLabel("Answer: ");
JTextField aField = new JTextField(16);
JLabel result = new JLabel();
JButton submit = new JButton("Submit");
JButton reset = new JButton("Reset");
JButton ng = new JButton("New Game");
final Random rand = new Random();
final int randPairNo = rand.nextInt(50);
final Rng rngOb1 = new Rng();
final int[] shuffList = rngOb1.numFinder();
public Gui() {
super("State Capitals Game");
setLookAndFeel();
setSize(325, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.insets = new Insets(5, 5, 5, 5);
gc.gridx = 0;
gc.gridy = 0;
add(instructions, gc);
gc.gridx = 0;
gc.gridy = 1;
add(aLabel, gc);
gc.gridx = 0;
gc.gridy = 2;
add(aField, gc);
gc.gridx = 0;
gc.gridy = 3;
add(result, gc);
gc.gridwidth = 3;
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 0;
gc.gridy = 4;
add(submit, gc);
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String userIP = aField.getText();
String mg = StateData.caps[randPairNo];
if (userIP.equalsIgnoreCase(mg)) {
result.setText("That's correct!");
} else {
result.setText("Sorry, that's incorrect.");
}
}
});
gc.gridwidth = 3;
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 0;
gc.gridy = 5;
add(reset, gc);
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
aField.setText("");
}
});
gc.gridwidth = 3;
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 0;
gc.gridy = 6;
add(ng, gc);
//This is the new game button I'm trying to get to work.
ng.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int counter = 0;
if (counter < shuffList.length) {
instructions.setText("What is the capital of "
+ StateData.states[shuffList[counter]] + "?");
counter++;
} else {
counter = 0;
}
}
});
}
private void setLookAndFeel() {
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception exc) {
// ignore error
}
}
}
import java.util.Random;
public class Rng {
int[] numFinder() {
int[] list = new int[50];
for (int i = 0; i < list.length; i++) {
list[i] = i;
}
Random rgen = new Random();
for (int i = 0; i < list.length; i++) {
int rnd = rgen.nextInt(list.length);
int temp = list[i];
list[i] = list[rnd];
list[rnd] = temp;
}
return list;
}
}
public class StateData {
public static String[] states = {"Alabama", "Alaska", "Arizona", "Arkansas", "California",
"Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii",
"Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisianna",
"Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi",
"Missouri", "Montana", "Nebraska", "Nevada", "New Hampsire", "New Jersey",
"New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma",
"Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
"Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia",
"Winsconsin", "Wyoming" };
public static String [] caps = {"Montgomery", "Juneau", "Pheonix", "Little Rock", "Sacremento", "Denver",
"Hartford", "Dover", "Talahasee", "Atlanta", "Honolulu", "Boise", "Springfield", "Idianapolis",
"Des Moines", "Topka", "Franfort", "Baton Rouge", "Augusta", "Annapolis", "Boston", "Lansing",
"St Paul", "Jackson", "Jefferson City", "Helena", "Lincoln", "Carson City", "Concord",
"Trenton", "Santa Fe", "Albany", "Raleigh", "Bismarck", "Columbus", "Oklahama City",
"Salem", "Harrisburg", "Providence", "Columbia", "Pierre", "Nashville", "Austin", "Salt Lake City",
"Montpelier", "Richmond", "Olympia", "Charleston", "Madison", "Cheyenne"};
}
答案 0 :(得分:4)
你的问题似乎在于ActionListener。
public void actionPerformed(ActionEvent e) {
int counter = 0;
每按一次按钮,这将重置计数器。与您的描述一致。
将计数器声明为Gui类中的一个字段,你应该没问题。