I'm trying to make a program with a 'new game' button which when pressed asks the user a new question. It's a question about state capitals and I want each new game to just change the name of the state in the question. This is probably a dumb way to do it but I'm just experimenting and trying to learn more about java.
我正在尝试用一个“新游戏”按钮制作一个程序,当按下这个按钮时,它会问用户一个新问题。这是一个关于州首府的问题,我希望每个新游戏都能在这个问题中改变州名。这可能是一种愚蠢的方法,但我只是在试验并尝试学习更多关于java的知识。
The problem I have is that it only works once. I press new game and it changes ok the first time and then it does nothing. I've tried using various combination of while and for loops but with no luck. Sometimes the program even crashed when I tried using counter++ in some combination of for loop! So, yeah I'm pretty stuck so any hel would be appreciated greatly thanks.
我的问题是它只能工作一次。我按下新游戏,它第一次就改变了,然后就什么都不做了。我尝试过使用while和for循环的各种组合,但是没有运气。有时当我尝试在for循环中使用计数器+时,程序甚至会崩溃!所以,是的,我被困住了,所以我非常感谢你。
Main
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);
}
}
Gui
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
}
}
}
Rng
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;
}
}
StateData
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"};
}
1 个解决方案
#1
4
Your problem seems to lie in the ActionListener. You have
你的问题似乎在于ActionListener。你有
public void actionPerformed(ActionEvent e) {
int counter = 0;
This will reset counter each time the button is pressed. Consistent with your description.
每次按下按钮时,将重置计数器。与你的描述一致。
Declare counter as a field in your Gui class and you should be fine.
将counter声明为Gui类中的一个字段,应该没问题。
#1
4
Your problem seems to lie in the ActionListener. You have
你的问题似乎在于ActionListener。你有
public void actionPerformed(ActionEvent e) {
int counter = 0;
This will reset counter each time the button is pressed. Consistent with your description.
每次按下按钮时,将重置计数器。与你的描述一致。
Declare counter as a field in your Gui class and you should be fine.
将counter声明为Gui类中的一个字段,应该没问题。