目录
1.代码地址和博客地址
代码地址:https://github.com/wuxuezeproject/-2
领航员的博客地址:https://www.cnblogs.com/yanggang98/p/10801654.html
2.设计思路
2.1 基本思想
本次作业,我是驾驶员,主要负责代码的编写和图形用户界面的实现,用的语言是Java语言,属于现学现卖,但也颇有收获。
本次作业可以分为两大类,一是随机题目的生成,二是图形画界面。具体实现如下:
2.2 随机题目的生成
2.2.1 随机产生符号和数字
shuji[0]=getnum();
for(int i=1;i<=(int)(Math.random()*3)+1;i++)
{
shuji[i]=getnum();
getchar();
fuhaoji=fuhaoji+fuhao_sel;
}
private double getnum()
{
double a=(Math.random()*max)+1;
a=((int)(a*100))/100.0;
return (a);
}
public void getchar()
{
int le=(int)(Math.random()*(fuhao.length()-1))+1;
fuhao_sel=fuhao.substring(le, le+1);
}
2.2.2 括号的添加
int id=Math.abs((int)(Math.random()*(fuhaoji.length()-1)));
String a,b,c;
a=fuhaoji.substring(0, id+1);
b=fuhaoji.substring(id+1,id+2);
if ((id+2) < (fuhaoji.length())) c=fuhaoji.substring(id+2,fuhaoji.length());
else c="";
fuhaoji=a+"("+b+")"+c;
2.2.3 题目的生成
private void show()
{
int no=1,ni=0;
ti=" ";
if (kuohao==1&&fuhaoji.charAt(1)=='(')
{
while(true)
{
if (no>=fuhaoji.length()) break;
ti=ti+fuhaoji.substring(no,no+1);
if (fuhaoji.charAt(no)==')')
{
no++;
if (no>=fuhaoji.length()) break;
ti=ti+fuhaoji.substring(no,no+1);
}
if (xiaoshu==1) ti=ti+String.valueOf(shuji[ni]);
else ti=ti+String.valueOf((int)shuji[ni]);
ni++;no++;
if (no==fuhaoji.length()) break;
}
}
else if (kuohao==1&&fuhaoji.charAt(1)!='(')
{
if (xiaoshu==1) ti=ti+String.valueOf(shuji[0]);
else ti=ti+String.valueOf((int)shuji[0]);
ni++;
while(true)
{
if (no>=fuhaoji.length()) break;
ti=ti+fuhaoji.substring(no,no+1);
no++;
if (no>=fuhaoji.length()) break;
if (fuhaoji.charAt(no)=='(')
{
//no++;
ti=ti+fuhaoji.substring(no,no+1);
}
else no--;
if (fuhaoji.charAt(no)==')'&&no<fuhaoji.length())
{
no++;
ti=ti+fuhaoji.substring(no,no+1);
}
if (xiaoshu==1) ti=ti+String.valueOf(shuji[ni]);
else ti=ti+String.valueOf((int)shuji[ni]);
ni++;no++;
if (no==fuhaoji.length()) break;
}
}
else if(kuohao==0)
{
if (xiaoshu==1) ti=ti+String.valueOf(shuji[ni]);
else ti=ti+String.valueOf((int)shuji[ni]);
ni++;
while(true)
{
if (no>=fuhaoji.length()) break;
ti=ti+fuhaoji.substring(no,no+1);
if (xiaoshu==1)
{ti=ti+String.valueOf(shuji[ni]);}
else
{ti=ti+String.valueOf((int)shuji[ni]);}
ni++;
no++;
if (no>=fuhaoji.length()) break;
}
}
}
2.2.4 计算结果
private void count()
{
int no=1,ni=0,top1=0,top2=0;
double a[]={0,0,0,0};
char[] b=new char[5];
a[top1]=shu_new[ni];
top1++;
ni++;
while(true)
{
if (no>=fuhao_new.length()) break;
char ch=fuhao_new.charAt(no);
no++;
if (ch=='*'||ch=='/')
{
double s=shu_new[ni];
ni++;
if (ch=='*') a[top1-1]=a[top1-1]*s;
else if (ch=='/') a[top1-1]=a[top1-1]/s;
}
else if (ch=='+'||ch=='-')
{
double s=shu_new[ni];
ni++;
if (top2==1&&top1==2)
{
if (b[0]=='+') a[0]=a[0]+a[1];
else if (b[0]=='-') a[0]=a[0]-a[1];
b[1]=ch;
a[1]=s;
}
else
{
b[top2]=ch;
top2++;
a[top1]=s;
top1++;
}
}
// else if (fuhaoji.charAt(no+1)!='(');
}
if (top2==1&&top1==2)
{
if (b[0]=='+') a[0]=a[0]+a[1];
else if (b[0]=='-') a[0]=a[0]-a[1];
}
if (xiaoshu==1) daan=String.valueOf(a[0]);
else daan=String.valueOf((int)a[0]);
}
2.3 图形画界面的实现
2.3.1 框架代码
package cn.window.java;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStream;
import java.io.Writer;
public class face_1 extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JCheckBox checkbox[];
private JComboBox<Integer> comb_num,comb_max;
private int num=1;
private int max=10;
private int style =0;
private int iss=0;
private JButton bcreat,bput;
private JButton[] submit = new JButton[20];
private JTextField[] timu=new JTextField[20],ans1=new JTextField[20],ans2=new JTextField[20],ans3=new JTextField[20];
private test[] t=new test[20];
public face_1()
{
super("简单四则运算");
this.setSize(900,600);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
JToolBar toolbar = new JToolBar();
this.getContentPane().add(toolbar, "North");
String str[][]={{"题目个数 :"},{"+","-","*","/","()","小数"},{" 最大值:"}};
toolbar.add(new JLabel(" "));
toolbar.add(new JLabel(str[0][0]));
Integer num1[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
this.comb_num=new JComboBox<Integer>(num1);
this.comb_num.addActionListener(this);
toolbar.add(this.comb_num);
toolbar.add(new JLabel(" 选择运算符和数据类型:"));
this.checkbox=new JCheckBox[str[1].length];
for (int i=0;i<str[1].length;i++)
{
this.checkbox[i]=new JCheckBox(str[1][i]);
toolbar.add(this.checkbox[i]);
this.checkbox[i].addActionListener(this);
}
toolbar.add(new JLabel(" "));
toolbar.add(new JLabel(str[2][0]));
Integer max1[]={10,50,100,1000,10000};
this.comb_max=new JComboBox<Integer>(max1);
this.comb_max.addActionListener(this);
toolbar.add(this.comb_max);
toolbar.add(new JLabel(" "));
this.bcreat=new JButton(" 生成 ");
this.bcreat.addActionListener(this);
toolbar.add(bcreat);
toolbar.add(new JLabel(" "));
this.bput=new JButton(" 打印 ");
this.bput.addActionListener(this);
toolbar.add(bput);
toolbar.add(new JLabel(" "));
JPanel equ=new JPanel();
this.getContentPane().add(equ, "Center");
equ.setLayout(new GridLayout(21, 7, 5, 5));
equ.add(new JLabel(" 题号") );
equ.add(new JLabel(" 题目") );
equ.add(new JLabel(" 我的答案") );
equ.add(new JLabel(" 提交") );
equ.add(new JLabel(" 结果") );
equ.add(new JLabel(" 正确答案") );
String no[]={" 1"," 2"," 3"," 4"," 5"," 6"," 7"," 8"," 9"," 10"," 11"," 12"," 13"," 14"," 15"," 16"," 17"," 18"," 19"," 20"};
for (int i=0;i<20;i++)
{
equ.add(new JLabel (no[i]));
this.timu[i]=new JTextField ("题目在这儿!");
equ.add(this.timu[i]);
this.ans1[i]=new JTextField ();
this.ans1[i].addActionListener(this);
equ.add(this.ans1[i]);
this.submit[i]=new JButton("提交");
this.submit[i].addActionListener(this);
equ.add(this.submit[i]);
this.ans2[i]=new JTextField ("对不对提交一下才知道");
this.ans2[i].addActionListener(this);
equ.add(this.ans2[i]);
this.ans3[i]=new JTextField ("正确答案看这儿!");
this.ans3[i].addActionListener(this);
equ.add(this.ans3[i]);
}
this.setVisible(true);
}
private void submit(int n)
{
if (n>=num)
{
JOptionPane.showMessageDialog(this, "此处没有题目哦,不能提交!");
return;
}
String str=this.ans1[n].getText();
if (str.equals(""))
{
JOptionPane.showMessageDialog(this, "不写答案还有勇气提交?");
return;
}
String s=t[n].daan;
if (str.equals(s)) //
{
this.ans2[n].setCaretColor(Color.blue);
this.ans2[n].setText("正确");
}
else
{
this.ans2[n].setCaretColor(Color.red);
this.ans2[n].setText("错误");
}
this.ans3[n].setText(t[n].daan);
this.ans3[n].setCaretColor(Color.darkGray);
}
public void bcreatsx()
{
if (style==0)
{
JOptionPane.showMessageDialog(this, "还没有选择符号哦,不能生成!");
return;
}
if (style==16||style==32)
{
if (style==16)JOptionPane.showMessageDialog(this, "小朋友,只有括号是不行滴!");
if (style==32)JOptionPane.showMessageDialog(this, "小朋友,只有小数是不行滴!");
return;
}
JOptionPane.showMessageDialog(this, "原有题目就没有了哦!");
for (int i=0;i<num;i++)
{
this.ans1[i].setText("");
this.ans2[i].setText("对不对提交一下才知道");
this.ans3[i].setText("正确答案看这儿!");
t[i]=new test(max,style);//
t[i].creat();
this.timu[i].setText(t[i].ti);
}
iss++;
}
public void charOutStream() throws Exception{
// 1:利用File类找到要操作的对象
File file = new File("D:" + File.separator + "JAVA" + File.separator + "test.txt");
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
Writer out = new FileWriter(file);
for (int i=0;i<num;i++)
{
out.write("题目:");
out.write(t[i].ti);
out.write(" 正确答案:");
out.write(t[i].daan);
out.write(" 我的答案:");
out.write(this.ans1[i].getText());
out.write("\r\n");
}
out.close();
JOptionPane.showMessageDialog(this, "已经打印成功了!在D:JAVA:test.txt中。");
}
public void actionPerformed(ActionEvent ev)
{
if (ev.getSource()==this.bcreat)
{
bcreatsx();//
}
if (ev.getSource()==this.bput)
{
if (iss==0)
{
JOptionPane.showMessageDialog(this, "还没有题目哦,不能打印!");
return ;
}
try {
charOutStream();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//dayin
}
if (ev.getSource()instanceof JComboBox<?>||ev.getSource()instanceof JCheckBox||ev.getSource()instanceof JMenuItem)
{
Object obj1=this.comb_num.getSelectedItem();
num=((Integer)obj1).intValue();
Object obj2=this.comb_max.getSelectedItem();
max=((Integer)obj2).intValue();
if (ev.getActionCommand().equals("+")) style=style^1;
if (ev.getActionCommand().equals("-")) style=style^2;
if (ev.getActionCommand().equals("*")) style=style^4;
if (ev.getActionCommand().equals("/")) style=style^8;
if (ev.getActionCommand().equals("()")) style=style^16;
if (ev.getActionCommand().equals("小数")) style=style^32;
}
if (ev.getSource()==submit[0]) submit(0);
else if (ev.getSource()==submit[1]) submit(1); //
else if (ev.getSource()==submit[2]) submit(2);
else if (ev.getSource()==submit[3]) submit(3);
else if (ev.getSource()==submit[4]) submit(4);
else if (ev.getSource()==submit[5]) submit(5);
else if (ev.getSource()==submit[6]) submit(6);
else if (ev.getSource()==submit[7]) submit(7);
else if (ev.getSource()==submit[8]) submit(8);
else if (ev.getSource()==submit[9]) submit(9);
else if (ev.getSource()==submit[10]) submit(10);
else if (ev.getSource()==submit[11]) submit(11);
else if (ev.getSource()==submit[12]) submit(12);
else if (ev.getSource()==submit[13]) submit(13);
else if (ev.getSource()==submit[14]) submit(14);
else if (ev.getSource()==submit[15]) submit(15);
else if (ev.getSource()==submit[16]) submit(16);
else if (ev.getSource()==submit[17]) submit(17);
else if (ev.getSource()==submit[18]) submit(18);
else if (ev.getSource()==submit[19]) submit(19);
}
public static void main(String args[])
{
new face_1();
}
}
2.3.2 框架截图
2.3.3 符号选择错误异常处理
2.3.4 提交答案异常处理
2.3.5 打印异常处理
2.4 正常运行结果
2.4.1 题目正常提交
2.4.2 正常打印结果
2.4.3 打印后预览
3. 结对收获及评价
3.1 和伙伴工作时的截图
3.2 对伙伴的评价
本次我们这个项目的的领航员是杨港同学。
在作业的初期,我们两个进行了计划的分配,我是驾驶员,他是领航员。后来我们一起构造工作的思路,各部分功能的实现,以及图形画界面的布局等等。最后制定出了完整的开发计划。他在初期设计工作中起到了关键性的作用,和他合作也是非常的愉快。
在编程工作中,领航员能够认真的对我的工作以及代码进行很好的监督,也给我提出了许多很有效的编码技巧和部分功能的实现方法,能够及时和我讨论出现的问题。
在编程结束后,领航员能够和我进行程序的审核,看看各部分功能是否都已经实现,提出改进的建议。代码写完后也能很负责的进行单元测试。
总的来说,这次的结对编程,领航员起到了至关重要的作用。从初期的设计,中期的编码,以及后期的测试等,如果没有领航员的监督指导,一个人是很难很好的完成这么多的工作的。也非常感谢能够和他一起合作。
4.个人收获
本次作业,使我从一个Java小白晋级为一个Java入门,可以说是又接触了一门新的语言。颇有收获。
以往的的作业,课设等我们用的都是小黑框,每次面对小黑框,都会产生一个疑惑,在大学的这三年多的学习中,一直都是小黑框,面对每天接触的那么多形形色色的软件,功能那么强大,但我只能做小黑框,没有一件可以拿的出手的东西,外加前几天去招聘会,和一个招聘老师谈了谈,当她问到我们平时都有没有做过什么,我们只能摇头的时候,就决定做一个可以拿得出手的作品。
所以捞起了一本Java的书开始现学现卖。完成了本次的作业,收获满满,而且以后也不用天天面对小黑框了。