小学数学闯关游戏 java代码_简单的java程序 小学数学闯关游戏 多谢高分
展开全部
有4个类 MainFrame,Question,QuestionPanel,ResultPanel
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
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() {
e69da5e6ba903231313335323631343130323136353331333264663730this.setTitle("数学测试0.1");
this.setBounds(100, 100, M_WIDTH, M_HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.initialization();
showingPanel = new JPanel();
start = new JButton("开始");
ButtonMonitor bm = new ButtonMonitor();
start.addActionListener(bm);
submit = new JButton("提交");
submit.addActionListener(bm);
next = new JButton("下一关");
next.addActionListener(bm);
toolBar = new JPanel();
toolBar.add(start);
this.add(toolBar, BorderLayout.SOUTH);
this.setVisible(true);
}
public void initialization() {
level = 1;
scores = new int[MAX_LEVEL];
questions = new QuestionPanel[SIZE];
first = true;
}
public void doQuestion() {
this.remove(showingPanel);
toolBar.remove(next);
if (first) {
toolBar.remove(start);
}
showingPanel = new JPanel();
showingPanel.setLayout(new GridLayout(SIZE, 0));
for (int i = 0; i < SIZE; i++) {
questions[i] = new QuestionPanel(new Question(level));
showingPanel.add(questions[i]);
}
this.add(showingPanel, BorderLayout.CENTER);
toolBar.add(submit);
showingPanel.updateUI();
toolBar.updateUI();
}
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;
System.out.println(sum);
if (sum > flag) {
level++;
this.doQuestion();
} else {
JOptionPane.showMessageDialog(null, "你分数没高过" + (flag) + "游戏结束");
}
} else {
JOptionPane.showMessageDialog(null, "游戏结束你总分:" + sum);
}
}
public void doSubmit() {
int sum = 0;
for (int i = 0; i < SIZE; i++) {
String temp = questions[i].getAnswer().getText();
if (!temp.isEmpty()) {
try {
int answer = Integer.parseInt(temp);
if (answer == questions[i].getQuestion().getAnswer()) {
sum += 100;
}
} catch (Exception e) {
}
}
}
scores[level - 1] = sum;
this.remove(showingPanel);
showingPanel = new ResultPanel(level, scores);
this.add(showingPanel, BorderLayout.CENTER);
toolBar.remove(submit);
toolBar.add(next);
showingPanel.updateUI();
toolBar.updateUI();
}
/**
* @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 = e.getActionCommand();
if (key.equals("提交")) {
doSubmit();
} else if (key.equals("开始")) {
doQuestion();
} else if (key.equals("下一关")) {
doNext();
}
}
}
}
import java.util.Random;
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 (r.nextBoolean()) {
modifior = 0;
first = r.nextInt(10) + 1;
second = r.nextInt(10) + 1;
answer = first + second;
} else {
modifior = 1;
first = r.nextInt(20) + 1;
second = r.nextInt(first) + 1;
answer = first - second;
}
break;
case 2:
first = r.nextInt(9) + 1;
second = r.nextInt(9) + 1;
if (r.nextBoolean()) {
modifior = 2;
answer = first * second;
} else {
modifior = 3;
answer = first;
first = first * second;
}
break;
case 3:
modifior = r.nextInt(4);
switch (modifior) {
case 0:
first = r.nextInt(40)+10;
second = r.nextInt(40)+10;
answer = first + second;
break;
case 1:
first = r.nextInt(80)+20;
second = r.nextInt(first-10)+11;
answer = first -second;
break;
case 2:
first = r.nextInt(10)+1;
second = r.nextInt(10)+1;
answer = first * second;
break;
case 3:
first = r.nextInt(10)+1;
second = r.nextInt(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) {
this.first = first;
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
}
public int getModifior() {
return modifior;
}
public void setModifior(int modifior) {
this.modifior = modifior;
}
public int getAnswer() {
return answer;
}
public void setAnswer(int answer) {
this.answer = answer;
}
}
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class QuestionPanel extends JPanel {
private JLabel questionLabel;
private JTextField answer;
private Question question;
QuestionPanel(Question question) {
this.question = question;
questionLabel = new JLabel();
questionLabel.setText(this.question.toQuestionForm());
answer = new JTextField(2);
this.add(questionLabel);
this.add(answer);
}
public JLabel getQuestionLabel() {
return questionLabel;
}
public JTextField getAnswer() {
return answer;
}
public Question getQuestion() {
return question;
}
public void setLabel(String label){
this.questionLabel.setText(label);
}
public void setQuestion(Question question){
this.question = question;
}
public void setAnswer(String text){
this.answer.setText(text);
}
}
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ResultPanel extends JPanel {
private int level;
private int [] scores;
ResultPanel(int level,int [] scores){
this.level = level;
this.scores = scores;
this.setLayout(new GridLayout(MainFrame.MAX_LEVEL*2+1, 0));
for(int i=0;i
JPanel temp1 = new JPanel();
temp1.add(new JLabel("第"+(i+1)+"关分数"));
JPanel temp2 = new JPanel();
temp2.add(new JLabel(""+scores[i]));
this.add(temp1);
this.add(temp2);
}
}
}
已赞过
已踩过<
你对这个回答的评价是?
评论
收起
小学数学闯关游戏 java代码_简单的java程序 小学数学闯关游戏 多谢高分相关推荐
- 日记本java代码_简单的JAVA日记本程序源代码
[实例简介] 一个入门级的JAVA程序源代码,界面绝对赞,很Q的日记本,原理不复杂,代码注释非常详尽清晰,一看就懂.适合初学者. [实例截图] [核心代码] JAVA日记本程序 └── yang ├─ ...
- 高斯模糊java代码_简单的java高斯模糊算法
importjava.awt.Color;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.IOException ...
- 红牛农场java代码_实验题目 Java语言概述.doc
实验题目 Java语言概述 实验一 Java语言概述 [实验目的] 1.掌握开发Java应用程序的三个步骤:编写源文件.编译源文件和运行应用程序. 2.熟悉Java应用程序的基本结构,并能联合编译应用 ...
- java 抽奖_简单实现java抽奖系统
导读热词 本文为大家分享了java抽奖系统的具体代码,供大家参考,具体内容如下 用户信息类 /* * 用户信息类 * 1.账号 * 2.密码 * 3.卡号 * 4.是否登录 */ public cla ...
- 简单的秒表计时器java报告_简单的Java秒表计时器(线程)
秒表的个个位数的计算: package Seconds; public class Watch_time extends Thread{ int ms,ms_1,ms_2; int s,s_1; in ...
- 有哪些小游戏的java代码_求一个Java小游戏代码(鼠标点击类小游戏)最好代码里面不要有中文...
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 } else{ black_count=0; } if(chess[i][j]==2) { white_count++; if(white_count== ...
- 老司机写的java代码_据说每个JavaEE程序员都是老司机
JavaEE老司机的真实路况 JavaEE老司机期望的路况 修路的艰辛 JavaEE程序员梦想的路 为什么愿景很美好,道路很崎岖? JavaEE开发的现状 我们的工具 html 是浏览器标记语言 cs ...
- 井字游戏java代码_井字游戏(java)
井字游戏(java) 井字游戏(java) 分析:游戏方式只有赢和平两种方式 赢:有一方有3个连成一条线就赢了(横,竖,对角线).想要赢填子的数量必须大于等于5,所以填前面4个格子时不需要判断. 考虑 ...
- 去停用词 java代码_如何在java中去除中文文本的停用词
1. 整体思路 第一步:先将中文文本进行分词,这里使用的HanLP-汉语言处理包进行中文文本分词. 第二步:使用停用词表,去除分好的词中的停用词. 2. 中文文本分词环境配置 使用的HanLP-汉 ...
最新文章
- 递增的整数序列链表的插入_每日算法题 | 剑指offer 链表专题 (5)链表中倒数第k个节点...
- centerface
- C# 移除数组中重复项
- postman面试_Postman 收费太贵了,我决定用 Postwoman。。。
- Boost:std ::bind与Boost的_1绑定的测试程序
- 贤惠限量,请妥善使用
- 阿里巴巴开源 Sentinel,进一步完善 Dubbo 生态
- PAT-乙级-1062 最简分数
- 安裝jpeg-6b png error错误解决方法
- python中json模块_Python json模块与jsonpath模块区别详解
- explain如何查看mysql_MySql中如何使用 explain 查询 SQL 的执行计划
- Kotlin中正则表达式分析
- 为什么是GCN(转)
- 基于深度学习模型+Attention机制的分类模型构建实践分析【以鸢尾花数据集为例】
- linux7配置永久路由,CentOS7添加永久静态路由
- java学生签到系统_学生签到系统.pdf
- Neo4j CQL语法
- 朋友圈点赞测试用例~~~脑图
- RTL8188CUS 无线网卡使用说明
- htc android sd卡,HTC M8支持扩展储存卡吗?HTC M8支持多大的储存卡?
热门文章
- python爬虫大全
- 干货 | 选择“正激”还是“反激”?这份宝典请收好~
- 第27课:Node.js 模块详解
- 一种简单的图像LDR->HDR算法
- 萧山朝阳计算机学校,萧山日报数字报-朝阳8号等三个小区加入双学区队列
- 全球及中国转子平衡机行业发展潜力与及十四五竞争战略研究报告2022版
- 嵌入式开发也可以用C++?
- 【C++学习】类与对象(下)
- ZCMU 5260: 魔法咒语(贪心)
- Fusing time-of-flight depth and color for real-time segmentation and tracking