我正在尝试制作一个数学游戏,其中用户键入问题编号(1-8),并显示一个输入框,以便实际存储他们输入的内容。我在输入框和按键输入上遇到了困难,非常感谢您的帮助。我试图建立一个if状态,就像用户输入数字1一样,它将要求用户为问题一输入正确的答案,如果用户输入正确,它将加一个点,如果输入错误,它将被加分。加1死亡。用户只能将每个问题查看一次。
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.awt.*;
import java.applet.*;
import javax.swing.*;
public class Java_Math_Game extends Applet {
private static volatile boolean wPressed = false;
public static boolean isWPressed() {
synchronized (Java_Math_Game.class) {
return wPressed;
}
}
Image offScreen;
Graphics offG;
Image background;
AudioClip music;
String answer;
int lives = 3;
int score = 0;
int points = 0;
int q1 = 2400;
int answer1;
public void init() {
offScreen = createImage(500,500);
offG = offScreen.getGraphics();
background = getImage(getCodeBase(),"background1.jpg" );
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(background, 0);
while(tracker.checkAll(true) != true){ }
if (tracker.isErrorAny()){
JOptionPane.showMessageDialog(null, "Trouble loading pictures.");
AudioClip music = getAudioClip(getDocumentBase(), "Soulful.wav");
music.play();
}
}
public void paint(Graphics g) {
g.drawImage(background,0,0,this);
g.setFont(new Font("TimesRoman", 1, 25));
g.drawString("Type question number to choose your question",200,30);
g.drawString("Question 1 60 x 40 = ?",20,100);
g.drawString("Question 2 10 + 830 = ?",20,300);
g.drawString("Question 3 647 - 416 = ?",20,500);
g.drawString("Question 4 10 / 2 = ?",320,100);
g.drawString("Question 5 120 / 40 = ?",320,300);
g.drawString("Question 5 400 + 800 = ?",320,500);
g.drawString("Question 6 8.3 x 8.3 = ?",620,100);
g.drawString("Question 7 123 - 123 = ?",620,300);
g.drawString("Question 8 50^2 = ?",620,500);
}
public boolean keyDown(Event e, int key) {
//Convert ASCII integer value to a String
String entered = ""+(char)key;
//Check if "d" or right arrow was pressed and change x direction
if (entered.equals("d"))
{
answer = JOptionPane.showInputDialog("Enter Answer");
answer1 = Integer.parseInt(answer);
}
return true;
}
}