四则运算生成(最近天气不错,太阳当空照)
四则运算生成
题目要求
本次作业要求两个人合作完成,驾驶员和导航员角色自定,鼓励大家在工作期间角色随时互换,这里会布置两个题目,请各组成员根据自己的爱好任选一题。
我们在刚开始上课的时候介绍过一个小学四则运算自动生成程序的例子,请实现它,要求:
能够自动生成四则运算练习题
可以定制题目数量
用户可以选择运算符
用户设置最大数(如十以内、百以内等)
用户选择是否有括号、是否有小数
用户选择输出方式(如输出到文件、打印机等)
最好能提供图形用户界面(根据自己能力选做,以完成上述功能为主)
队友及Coding
写代码的人吃喝玩乐:当然是我寄己了啦(Coding传送门)
写测试的人丰乳肥臀:睡在我下铺的肌肉男(肌肉男博客传送门)
以下是正文
作为一个对精通C和C++的人,我选择用JAVA实现。
以下为基本实现结构图:
以下为不怎么友好的图形化界面:
以下为图形化界面代码:
package swing;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Toolkit;
import javax.swing.JRadioButton;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class functionChoose extends JFrame {
private JPanel contentPane;
private JTextField numtxt;
private JTextField maxtxt;
/**
* Create the frame.
*/
public functionChoose() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//setBounds(100, 100, 450, 300);
setTitle("功能选择");
setSize(450, 300);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((screen.getWidth() - getWidth()) / 2);
int y = (int) ((screen.getHeight() - getHeight()) / 2);
setLocation(x, y);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
JLabel 括号 = new JLabel("\u62EC \u53F7");
括号.setFont(new Font("微软雅黑", Font.BOLD, 13));
括号.setBounds(10, 149, 35, 23);
contentPane.add(括号);
JRadioButton khY = new JRadioButton("Y");
khY.setFont(new Font("微软雅黑", Font.BOLD, 13));
khY.setBounds(56, 149, 35, 23);
contentPane.add(khY);
JRadioButton khN = new JRadioButton("N");
khN.setFont(new Font("微软雅黑", Font.BOLD, 13));
khN.setBounds(110, 149, 40, 23);
contentPane.add(khN);
ButtonGroup groupkuohao = new ButtonGroup();
groupkuohao.add(khY);
groupkuohao.add(khN);
JLabel 小数 = new JLabel("\u5C0F \u6570");
小数.setFont(new Font("微软雅黑", Font.BOLD, 13));
小数.setBounds(10, 182, 35, 23);
contentPane.add(小数);
JRadioButton xsY = new JRadioButton("Y");
xsY.setFont(new Font("微软雅黑", Font.BOLD, 13));
xsY.setBounds(56, 182, 35, 23);
contentPane.add(xsY);
JRadioButton xsN = new JRadioButton("N");
xsN.setFont(new Font("微软雅黑", Font.BOLD, 13));
xsN.setBounds(110, 182, 40, 23);
contentPane.add(xsN);
ButtonGroup groupxiaoshu = new ButtonGroup();
groupxiaoshu.add(xsY);
groupxiaoshu.add(xsN);
JLabel 运算符 = new JLabel("\u8FD0\u7B97\u7B26");
运算符.setFont(new Font("微软雅黑", Font.BOLD, 13));
运算符.setBounds(10, 76, 40, 23);
contentPane.add(运算符);
JCheckBox Add = new JCheckBox("+");
Add.setBounds(56, 76, 50, 23);
contentPane.add(Add);
JCheckBox Sub = new JCheckBox("-");
Sub.setBounds(110, 76, 50, 23);
contentPane.add(Sub);
JCheckBox Mult = new JCheckBox("*");
Mult.setBounds(56, 114, 50, 23);
contentPane.add(Mult);
JCheckBox Div = new JCheckBox("/");
Div.setBounds(110, 114, 50, 23);
contentPane.add(Div);
JLabel num = new JLabel("\u7B97\u5F0F\u4E2A\u6570");
num.setFont(new Font("微软雅黑", Font.BOLD, 13));
num.setBounds(10, 10, 60, 15);
contentPane.add(num);
JLabel max = new JLabel("\u6700\u5927\u6570");
max.setFont(new Font("微软雅黑", Font.BOLD, 13));
max.setBounds(10, 45, 65, 15);
contentPane.add(max);
numtxt = new JTextField();
numtxt.setFont(new Font("微软雅黑", Font.BOLD, 13));
numtxt.setBounds(80, 7, 66, 21);
contentPane.add(numtxt);
numtxt.setColumns(10);
maxtxt = new JTextField();
maxtxt.setFont(new Font("微软雅黑", Font.BOLD, 13));
maxtxt.setBounds(80, 42, 66, 21);
contentPane.add(maxtxt);
maxtxt.setColumns(10);
JLabel outMethod = new JLabel("\u8F93\u51FA\r\n\u65B9\u5F0F");
outMethod.setFont(new Font("微软雅黑", Font.BOLD, 13));
outMethod.setBounds(10, 220, 54, 15);
contentPane.add(outMethod);
JRadioButton outMethodF = new JRadioButton("\u6587\u4EF6");
outMethodF.setFont(new Font("微软雅黑", Font.BOLD, 13));
outMethodF.setBounds(70, 216, 60, 23);
contentPane.add(outMethodF);
JRadioButton outMethodP = new JRadioButton("\u6253\u5370\u673A");
outMethodP.setFont(new Font("微软雅黑", Font.BOLD, 13));
outMethodP.setBounds(130, 216, 70, 23);
contentPane.add(outMethodP);
ButtonGroup prin = new ButtonGroup();
prin.add(outMethodP);
prin.add(outMethodF);
JButton button = new JButton("\u751F\u6210\u7B97\u5F0F");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//算式个数num
String n1 = numtxt.getText();
int num = Integer.parseInt(n1);
//System.out.println("个数"+num);
//结果最大值max
String n2 = maxtxt.getText();
int max = Integer.parseInt(n2);
//System.out.println("最大值"+max);
//算式包含的运算符(+,-,*,/,括号,小数)
int[] a = {0,0,0,0};
int kh=0,outp=0,xs=0;
if(Add.isSelected())
a[0] = 1;
if(Sub.isSelected())
a[1] = 1;
if(Mult.isSelected())
a[2] = 1;
if(Div.isSelected())
a[3] = 1;
if(khY.isSelected()) {
kh = 1;
}else {
kh = 0;
}
if(xsY.isSelected()) {
xs = 1;
}else {
xs = 0;
}
if(outMethodF.isSelected()) {
outp = 1;
}else {
outp = 0;
}
//算式输出程序
output.out(num,max,a,kh,xs,outp);
}
});
button.setFont(new Font("微软雅黑", Font.BOLD, 13));
button.setBounds(299, 182, 93, 23);
contentPane.add(button);
}
}
以下为四则运算生成函数框图:
以下为四则运算生成式代码:
package swing;
import java.util.Random;
public class output {
public static void out(int n,int m,int[] A,int KH,int XS,int OUTP) {
/*System.out.print(n);
System.out.print(m);
System.out.print(A[0]);
System.out.print(A[1]);
System.out.print(A[2]);
System.out.print(A[3]);
System.out.print(KH);
System.out.print(XS);
System.out.println(OUTP);
*/
int[] B = new int[4];
B[0] = A[0];
B[1] = A[1];
B[2] = A[2];
B[3] = A[3];
int numOFfunction = 0,t = A[0] + A[1] + A[2] + A[3],l = 0,k = 0,j = 0;
double x;
Random number = new Random(); //产生随机整数
Random choose = new Random(); //产生随机数决定所用运算符
Random XSRan = new Random(); //随机决定是否产生小数
Random KHRan = new Random(); //随机产生括号
for(int i = 0;i < n;i ++) {
j = t;
k = 0;
l = 0;//循环产生指定数目的算式
numOFfunction = t;
A[0] = B[0];
A[1] = B[1];
A[2] = B[2];
A[3] = B[3];
if(KH == 1) {
if (t >= 2 && l == 0 && j > 2) {
if(KHRan.nextInt(2) == 1) {
System.out.print("(");
l++;
}
}
}
if(XS == 1) { //判断并产生小数
x = Math.random() + number.nextInt(m)+.12;
if(XSRan.nextInt(3) == 0) {
System.out.printf("%.2f",x);
if(l != 0) {
k++;
j--;
}
}
else {
System.out.print(number.nextInt(m)+1);
if(l != 0) {
k++;
j--;
}
}
}
else {
System.out.print(number.nextInt(m)+1); //第一个数
if(l != 0) {
k++;
j--;
}
}
if(k == 2) {
System.out.print(")");
k = 0;
l=0;
}
while(numOFfunction != 0) {
int ran;
while(true) {
ran = choose.nextInt(4);
if(A[ran] == 1) {
A[ran] = 0;
break;
}
}
if(ran == 0) {
System.out.print("+");
numOFfunction = numOFfunction - 1;
}
else if(ran == 1) {
System.out.print("-");
numOFfunction = numOFfunction - 1;
}
else if(ran == 2 ) {
System.out.print("*");
numOFfunction = numOFfunction - 1;
}
else if(ran == 3) {
System.out.print("/");
numOFfunction = numOFfunction - 1;
}
if(KH == 1) {
if (t >= 2 && l ==0 && j > 2) {
if(KHRan.nextInt(2) == 1) {
System.out.print("(");
l++;
}
}
}
if(XS == 1) {
x = Math.random() + number.nextInt(m)+.12;
if(XSRan.nextInt(3) == 0) {
System.out.printf("%.2f",x);
if(l != 0) {
k++;
j--;
}
}
else {
System.out.print(number.nextInt(m)+1);
if(l != 0) {
k++;
j--;
}
}
}
else {
System.out.print(number.nextInt(m)+1);
if(l != 0) {
k++;
j--;
}
}
if(k == 2) {
System.out.print(")");
k = 0;
l = 0;
}
}
System.out.println("=");
}
}
}
图形化界面实例化:
package swing;
import swing.functionChoose;
public class mainP {
public static void main(String[] args) {
functionChoose frame = new functionChoose();
frame.setVisible(true);
}
}
运行结果
输入
输出
代码测试
写测试的人丰乳肥臀:睡在我下铺的肌肉男(肌肉男博客传送门)
总结评价
正如老师上课所说,结对编程真的很有效率。两个人互相监督,提高了代码质量,减少差错,节省了很多时间。跟队友合作也是互相学习提高的过程。
在这次编程中用到了没接触过的Swing的相关知识,我们一起通过网络以及图书资源查找相关知识,最终实现了功能。人多力量大,而且队友很靠谱,遇到问题提出了很多有建设性的解决方案。