简单的java程序 小学数学闯关游戏 多谢高分

简单的java程序 小学数学闯关游戏 多谢高分,第1张

有4个类 MainFrame,Question,QuestionPanel,ResultPanel
import javaawtBorderLayout;
import javaawtGridLayout;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJOptionPane;
import javaxswingJPanel;
public class MainFrame extends JFrame {
public static final int M_WIDTH = 500, M_HEIGHT = 800;
public static final int SIZE = 10;
public static final int MAX_LEVEL = 3;
private int level;
private QuestionPanel[] questions;
private JPanel showingPanel, toolBar;
private int[] scores;
private JButton start, next, submit;
private boolean first;
MainFrame() {
thissetTitle("数学测试01");
thissetBounds(100, 100, M_WIDTH, M_HEIGHT);
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
thisinitialization();
showingPanel = new JPanel();
start = new JButton("开始");
ButtonMonitor bm = new ButtonMonitor();
startaddActionListener(bm);
submit = new JButton("提交");
submitaddActionListener(bm);
next = new JButton("下一关");
nextaddActionListener(bm);
toolBar = new JPanel();
toolBaradd(start);
thisadd(toolBar, BorderLayoutSOUTH);
thissetVisible(true);
}
public void initialization() {
level = 1;
scores = new int[MAX_LEVEL];
questions = new QuestionPanel[SIZE];
first = true;
}
public void doQuestion() {
thisremove(showingPanel);
toolBarremove(next);
if (first) {
toolBarremove(start);
}
showingPanel = new JPanel();
showingPanelsetLayout(new GridLayout(SIZE, 0));
for (int i = 0; i < SIZE; i++) {
questions[i] = new QuestionPanel(new Question(level));
showingPaneladd(questions[i]);
}
thisadd(showingPanel, BorderLayoutCENTER);
toolBaradd(submit);
showingPanelupdateUI();
toolBarupdateUI();
}
public void doNext() {
int sum = 0;
for (int i = 0; i < MAX_LEVEL; i++) {
sum += scores[i];
}
if (level != MAX_LEVEL) {
int flag = 500 level;
Systemoutprintln(sum);
if (sum > flag) {
level++;
thisdoQuestion();
} else {
JOptionPaneshowMessageDialog(null, "你分数没高过" + (flag) + "游戏结束");
}
} else {
JOptionPaneshowMessageDialog(null, "游戏结束你总分:" + sum);
}
}
public void doSubmit() {
int sum = 0;
for (int i = 0; i < SIZE; i++) {
String temp = questions[i]getAnswer()getText();
if (!tempisEmpty()) {
try {
int answer = IntegerparseInt(temp);
if (answer == questions[i]getQuestion()getAnswer()) {
sum += 100;
}
} catch (Exception e) {
}
}
}
scores[level - 1] = sum;
thisremove(showingPanel);
showingPanel = new ResultPanel(level, scores);
thisadd(showingPanel, BorderLayoutCENTER);
toolBarremove(submit);
toolBaradd(next);
showingPanelupdateUI();
toolBarupdateUI();
}
/
@param args
/
public static void main(String[] args) {
// TODO Auto-generated method stub
new MainFrame();
}
class ButtonMonitor implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String key = egetActionCommand();
if (keyequals("提交")) {
doSubmit();
} else if (keyequals("开始")) {
doQuestion();
} else if (keyequals("下一关")) {
doNext();
}
}
}
}
import javautilRandom;
public class Question {
private String[] operators = { "+", "-", "", "/" };
private int first;
private int second;
private int modifior;
private int answer;
Question(int level) {
Random r = new Random();
switch (level) {
case 1:
if (rnextBoolean()) {
modifior = 0;
first = rnextInt(10) + 1;
second = rnextInt(10) + 1;
answer = first + second;
} else {
modifior = 1;
first = rnextInt(20) + 1;
second = rnextInt(first) + 1;
answer = first - second;
}
break;
case 2:
first = rnextInt(9) + 1;
second = rnextInt(9) + 1;
if (rnextBoolean()) {
modifior = 2;
answer = first second;
} else {
modifior = 3;
answer = first;
first = first second;
}
break;
case 3:
modifior = rnextInt(4);
switch (modifior) {
case 0:
first = rnextInt(40)+10;
second = rnextInt(40)+10;
answer = first + second;
break;
case 1:
first = rnextInt(80)+20;
second = rnextInt(first-10)+11;
answer = first -second;
break;
case 2:
first = rnextInt(10)+1;
second = rnextInt(10)+1;
answer = first second;
break;
case 3:
first = rnextInt(10)+1;
second = rnextInt(10)+1;
answer = first;
first = first second;
break;
}
break;
}
}
public String toQuestionForm() {
String result = "" + first + " " + operators[modifior] +" " +second + " = ";
return result;
}
public int getFirst() {
return first;
}
public void setFirst(int first) {
thisfirst = first;
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
thissecond = second;
}
public int getModifior() {
return modifior;
}
public void setModifior(int modifior) {
thismodifior = modifior;
}
public int getAnswer() {
return answer;
}
public void setAnswer(int answer) {
thisanswer = answer;
}
}
import javaxswingJLabel;
import javaxswingJPanel;
import javaxswingJTextField;
public class QuestionPanel extends JPanel {
private JLabel questionLabel;
private JTextField answer;
private Question question;
QuestionPanel(Question question) {
thisquestion = question;
questionLabel = new JLabel();
questionLabelsetText(thisquestiontoQuestionForm());
answer = new JTextField(2);
thisadd(questionLabel);
thisadd(answer);
}

public JLabel getQuestionLabel() {
return questionLabel;
}
public JTextField getAnswer() {
return answer;
}
public Question getQuestion() {
return question;
}

public void setLabel(String label){
thisquestionLabelsetText(label);
}

public void setQuestion(Question question){
thisquestion = question;
}

public void setAnswer(String text){
thisanswersetText(text);
}
}
import javaawtGridLayout;
import javaxswingJLabel;
import javaxswingJPanel;
public class ResultPanel extends JPanel {
private int level;
private int [] scores;
ResultPanel(int level,int [] scores){
thislevel = level;
thisscores = scores;
thissetLayout(new GridLayout(MainFrameMAX_LEVEL2+1, 0));
for(int i=0;i<level;i++){
JPanel temp1 = new JPanel();
temp1add(new JLabel("第"+(i+1)+"关分数"));
JPanel temp2 = new JPanel();
temp2add(new JLabel(""+scores[i]));
thisadd(temp1);
thisadd(temp2);
}
}
}

以上就是关于简单的java程序 小学数学闯关游戏 多谢高分全部的内容,包括:简单的java程序 小学数学闯关游戏 多谢高分、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zz/9325537.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存