思则运算升级版

  • 题目:生成小学四则运算题目的 “软件”。 让程序能接受用户输入答案,并判定对错。 最后给出总共 对/错 的数量。
  • 需求分析
    1. 能自动生成加、减、乘、除四则运算式子,并在窗口中显现出来。
    2. 判断输入的答案是否正确,并给出正确答案。
    3. 能统计出一共做对和做错多少道题目。
    • 设计思路
    1. 做一个便于操作的界面,其中有两个随机按钮,用于给出随机数,有一个确定按钮,用于判断正误,并给出正确答案,和统计对错的个数。有一个取消按钮,用于取消输入的答案。一个符号按钮,由于选择加、减、乘、除。
    2. 做题时,只需点击两个随机按钮,一个选择按钮,输入答案,按确定键即可。
  • 代码与运行结果如下
import java.awt.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import java.util.Random;
import java.util.Scanner;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class sizeyunsuan implements ActionListener, ItemListener {
	JFrame f;
	JTextField first, second, third, last;
	JButton sub, cancel, button1, button2, button3, fourth, denyu;
	Box box1, box2, box3, box4;
	String s = null, w = null, m = null;
	Container con;
	String fuhao = "";
	Choice c2;
	int count = 0 ,count1=0;
	double temp;
	JTextArea textShow;

	public sizeyunsuan() {
		f = new JFrame();
		f.setTitle("欢迎进入王铭霞制作的四则运算测试");
		f.setSize(370, 320);
		f.setLocation(200, 200);

		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		con = f.getContentPane();

		box1 = Box.createHorizontalBox();

		first = new JTextField(5);
		c2 = new Choice();
		c2.addItemListener(this);
		c2.add("");
		c2.add("+");
		c2.add("-");
		c2.add("*");
		c2.add("/");
		third = new JTextField(5);
		fourth = new JButton("=");

		last = new JTextField(7);
		box1.add(first);
		box1.add(c2);
		box1.add(third);
		box1.add(fourth);
		box1.add(last);

		box2 = Box.createHorizontalBox();
		sub = new JButton("confirm");
		cancel = new JButton("cancel");
		button1 = new JButton("random1");
		button3 = new JButton("ramdom2");
		box2.add(button1);
		box2.add(Box.createHorizontalStrut(10));
		box2.add(button3);
		box2.add(Box.createHorizontalStrut(10));
		box2.add(sub);
		box2.add(Box.createHorizontalStrut(10));
		box2.add(cancel);
		sub.addActionListener(this);
		box3=Box.createHorizontalBox();
		textShow=new JTextArea (150,100);
		box3.add(textShow);

		box4 = Box.createVerticalBox();
		box4.add(Box.createVerticalStrut(20));
		box4.add(box1);
		box4.add(Box.createVerticalStrut(20));
		box4.add(box3);
		box4.add(Box.createVerticalStrut(20));
		box4.add(box2);
		box4.add(Box.createVerticalStrut(10));
		con.add(box4);

		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				s = String.valueOf(Math.round((Math.random() * 100)));
				first.setText(s);
			}
		});

		button3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				w = String.valueOf(Math.round((Math.random() * 100) + 1));
				third.setText(w);
			}
		});
		cancel.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() ==cancel )
					last.setText(null);

			}

		});

		f.setVisible(true);
	}

	public static void main(String arg[]) {
		sizeyunsuan w = new sizeyunsuan();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		double s1 = Integer.parseInt(first.getText());
		double s2 = Integer.parseInt(third.getText());
		double result = 0;
		if (fuhao.equals("+")) {
			result = s1 + s2;
			temp = Integer.parseInt(last.getText());
			if (temp == result) {
				JOptionPane.showMessageDialog(null, "congradulation");
				count=count+1;

			}
			if (temp != result) {
				JOptionPane.showMessageDialog(null, "sorry");
				count1=count1+1;

			}

		}
		else if (fuhao.equals("-")) {
			result = s1 - s2;

			temp = Integer.parseInt(last.getText());
			if (temp != result) {
				JOptionPane.showMessageDialog(null, "sorry");
				count1=count1+1;
			}

			if (temp == result) {
				JOptionPane.showMessageDialog(null, "congradulation");
				count=count+1;

			}
		}
		else if (fuhao.equals("*")) {
			result = s1 * s2;

			temp = Integer.parseInt(last.getText());
			if (temp == result) {
				JOptionPane.showMessageDialog(null, "congradulation");
				count=count+1;

			}
			if (temp != result) {
				JOptionPane.showMessageDialog(null, "sorry");
				count1=count1+1;

			}
		}
		else if (fuhao.equals("/")) {
			result = s1 / s2;

			temp = Integer.parseInt(last.getText());
			if (temp != result) {
				JOptionPane.showMessageDialog(null, "sorry");
				count1=count1+1;

			}
			if (temp == result) {
				JOptionPane.showMessageDialog(null, "congradulation");
				count=count+1;

			}

		}
		textShow.append(s1+""+fuhao+""+s2+"="+result+"\n");
		textShow.append("right"+count+"\n");
		textShow.append("wrong"+count1+"\n");
	}

	public void itemStateChanged(ItemEvent ie) {
		if (ie.getSource() == c2) {
			String str1 = c2.getSelectedItem();
			fanhui(str1);
		}
	}

	public String fanhui(String str2) {
		return fuhao = str2;
	}
}

  

时间: 2024-08-10 12:05:30

思则运算升级版的相关文章

关于思则运算的测试

#include<iostream>#include<ctime>#include<stdlib.h>using namespace std; void main(){ int begin=0; //子序列的开头        int end=0; //子序列的结尾        int newsum=0; //当前子序列最大值        srand((unsigned)time(NULL)); //生成随机数        int a[10000], n; cou

3月6号周五课堂练习:随机产生30个思则运算的题目

#include <stdlib.h> #include <iostream> #include <time.h> using namespace std; //随机产生30个四则运算题目的tesk void tesk() { int x, y, z; for (int i = 0; i < 30; i++){ x = rand() % 100; y = rand() % 100; z = rand() % 4; switch (z) { case(0) : co

随机生成思则运算2

package fee; import java.util.*; import java.util.Random; public class pp { static Scanner in = new Scanner(System.in); static Random r = new Random(); public static void main(String[] args) { System.out.println("答对了一提获得5分"); System.out.println(

华为海思智能手机处理器及其参数对比

从塞班到安卓,从单核到双核,从四核到八核,从3G到4G,从32位64位,手机技术的发展总是日新月异,最近几年华为手机处理器也得到了的肯定,现在华为主要竞争对手就是苹果和三星了,那么下面看看华为海思推出了哪些处理器: 海思 K3V2 ,是2012年业界体积最小处理器.K3V2有四个A9内核,16个GPU单元,使用TSMC 40nm工艺制造,面积12mmx12mm,是继英伟达tegra3和三星exynos4412之后第四款四核A9处理器.他是一款高性能CPU,主频分 为1.2GHz和1.5GHz,是

七牛的存储算法猜测

个人浏览网页的时候,有打标签的习惯.最近整理以往的标签的时候,发现积累了一些有关七牛公司存储策略的网页,遂决定整理一篇文章处理,以备记忆.当然,也希望对他人有用. 因为七牛公司的存储策略主要基于纠删码(Erasure Codes,EC),所以下面先从纠删码引申开来. 引言:何为纠删码 数据的爆炸式增长使得存储系统的规模不断增加,存储设备的可靠性却一直没有得到显著提高(SSD 从SLC 到MLC 和TLC 可靠性不断下降,磁盘随着单位面积写入数据更多导致可靠性无法提升),从而给数据的持久化存储带来

第二次作业——小学生四则运算

题目: 请编写一个能自动生成小学四则运算题目的 “软件”. 让程序能接受用户输入答案,并判定对错. 最后给出总共 对/错 的数量. 需求分析: ●基本功能 ●实现100以内的加法 ●实现100以内的减法 ●实现100以内的乘法 ●实现100以内的除法 ●累计答对题目的个数,并做出相关的评价 设计: ●首先选择是否进入测试状态,若选择进入测试,则随机产生100以内的加减乘除的思则运算的题目 ● 用Switch选择语句来选择做什么运算(0表示加法运算,1表示减法,2表示除法运算,3表示乘法)以及根据

中纪委机关报:警惕“腐败势力垮了可以歇了”杂音

大剑师有些迟疑最终沉声道飞儿是我找来的我对你仁至义尽了以后我们大路朝天各走一边天骄与我大剑师再无瓜葛 当我走出来的时候秦韵马上向旁边移动了一下空出了一小块地方给我笑道今天难得那么早下线过来坐坐吧 You wonder, continued the Marchese reflectively; "you wonder, no doubt, after hearing my opinions about the Contessa Morone, that I should care to marry

练习1----四则运算(升级版)

1/*完成了计算,用户输入(请老师正常地输入) * 缺点:校检输入还没有完成.真分数计算未能约分成最简分数,很奇怪,试了很久. *还有就是代码比较乱,觉得有些代码可以写成方法的形式(还未整理),日后会进一步改善. */ 2 package 软件工程; 3 import java.text.DecimalFormat; 4 /* 5 * 1.定制数量 6 * 2.控制是否有乘除法 7 * 3.控制数值范围 8 * 4.真分数练习题 9 */ 10 import java.util.Scanner;

【升级版】支持浮点型+-*/()运算的计算器

1 #include <iostream> 2 #include<sstream> 3 using namespace std; 4 template<typename T> 5 class stack 6 { 7 T p[40]; 8 int toop; 9 public: 10 stack() { toop = -1; } 11 void push(T t) { toop++; p[toop] = t; } 12 T top() { return p[toop];