软件工程个人作业(2)
?
源代碼:package zuoye2;
?
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
?
import javax.swing.*;
?
class jiemian
{
??? public jiemian()
??? {
??? JFrame? jf=new JFrame("四則運算");
??? jf.setBounds(200, 100, 500, 500);
??? jf.setLayout(null);
??? Container c=jf.getContentPane();
??? JCheckBox j1=new JCheckBox("有乘除法");
??? JCheckBox j2=new JCheckBox("有括號");
??? JCheckBox j3=new JCheckBox("有負數");
??? JCheckBox j4=new JCheckBox("有余數");
??? JLabel l1=new JLabel("請輸入數值范圍");
??? JLabel l4=new JLabel("請輸入參數個數(1—10)");
??? JLabel l2=new JLabel("—");
??? JLabel l3=new JLabel("請輸入題目數目(1-30)");
??? JTextField t1=new JTextField("0");
??? JTextField t2=new JTextField();
??? JTextField t3=new JTextField();
??? JTextField t4=new JTextField();
??? j1.setBounds(100, 100, 80,20);
??? j2.setBounds(100, 130, 80,20);
??? l4.setBounds(100, 160, 180,20);
??? j3.setBounds(100, 220, 80,20);
??? j4.setBounds(100, 250, 80,20);
??? t1.setBounds(100, 190, 50,20);
??? l1.setBounds(100, 280, 150,20);
??? t2.setBounds(100, 310, 50,20);
??? t3.setBounds(180, 310, 50,20);
??? l2.setBounds(160, 310, 30,20);
??? l3.setBounds(100, 340, 150,20);
??? t4.setBounds(100, 370, 50,20);
??? c.add(j1);
??? c.add(j2);
??? c.add(j3);
??? c.add(l4);
??? c.add(j4);
??? c.add(t1);
??? c.add(l1);
??? c.add(t2);
??? c.add(t3);
??? c.add(l2);
??? c.add(l3);
??? c.add(t4);
??? JButton b1=new JButton("確定");
??? JButton b2=new JButton("退出");
??? b1.setBounds(350, 150,70, 50);
??? b2.setBounds(350, 300,70, 50);
??? c.add(b1);
??? c.add(b2);
??? b1.addActionListener(new ActionListener() {
??????? @Override
??????? public void actionPerformed(ActionEvent e)
??????? {
??????????? jf.dispose();
??????????? String a1,a2,a3,a4,a5;
??????????? int c1,c2,c3,c4,c5;
??????????? boolean f1=false,f2=false,f3=false,f4=false,f5=false;
??????????? a1=t1.getText().toString();
??????????? a2=t2.getText().toString();
??????????? a3=t3.getText().toString();
??????????? a4=t4.getText().toString();
??????????? if(a2.equals("")||a3.equals(""))
??????????? {
??????????????? JOptionPane.showMessageDialog(null, "范圍不能為空", "范圍不能為空", JOptionPane.ERROR_MESSAGE);
??????????????? System.exit(0);
??????????? }
??????????? else if(a4.equals(""))
??????????? {
??????????????? JOptionPane.showMessageDialog(null, "數目不能為空", "數目不能為空", JOptionPane.ERROR_MESSAGE);
??????????????? System.exit(0);
??????????? }
??????????? else if(j2.isSelected())
??????????? {
??????????????? if (a1.equals(""))
??????????????? {
??? ????????????JOptionPane.showMessageDialog(null, "參數個數不能為空", "參數個數不能為空", JOptionPane.ERROR_MESSAGE);
??????????????? System.exit(0);
??????????????? }
??????????????? else if(Integer.parseInt(a1)>10||Integer.parseInt(a1)<=0)
??????????????????? {
??????????????????? JOptionPane.showMessageDialog(null, "參數個數超出", "參數個數超出", JOptionPane.ERROR_MESSAGE);
??????????????????? System.exit(0);}
??????????? }
??????????? else if(Integer.parseInt(a4)<=0||Integer.parseInt(a4)>30)
??????????? {
??????????????? JOptionPane.showMessageDialog(null, "題目數目不正確", "題目數目不正確", JOptionPane.ERROR_MESSAGE);
??????????????? System.exit(0);
??????????? }
??????????? else if(Integer.parseInt(a3)<=Integer.parseInt(a2))
??????????? {
??????????????? JOptionPane.showMessageDialog(null, "范圍不正確", "范圍不正確", JOptionPane.ERROR_MESSAGE);
??????????????? System.exit(0);
??????????? }
??????????? if(j1.isSelected())
??????????? {
??????????????? f1=true;
??????????? }
??????????? if(j2.isSelected())
??????????? {
??????????????? f2=true;
?? ?????????}
??????????? if(j3.isSelected())
??????????? {
??????????????? f3=true;
??????????? }
??????????? if(j4.isSelected())
??????????? {
??????????????? f4=true;
??????????? }
??????????? c1=Integer.parseInt(a1);
??????????? c2=Integer.parseInt(a2);
??????????? c3=Integer.parseInt(a3);
??????????? c4=Integer.parseInt(a4);
??????????? yunsuan a=new yunsuan(c1,c2,c3,c4,f1,f2,f3,f4);
??????? }
?
??? });
??? b2.addActionListener(new ActionListener() {
??????? @Override
??????? public void actionPerformed(ActionEvent e)
??????? {
??????????? jf.dispose();
??????? }
?
?
?
?
??? });
??? jf.setVisible(true);
??? jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
??? }
}
package zuoye2;
?
?
public class m extends jiemian
{
???
??? public static void main(String[] args)
??? {
??????? // TODO Auto-generated method stub
??????? jiemian a=new jiemian();
??? }
?
}
?
?
?
}
??????????? else
??????????? {
??????????????? if(e[i]==1)
??????????????? {
??????????????????? a[i]=r1[i].nextInt(c3-c2)+c2;
??????????????????? b[i]=r2[i].nextInt(c3-c2)+c2;
??????????????????? d[i]=a[i]+b[i];
??????????????????? a1[i]=String.valueOf(a[i]);
??????????????????? b1[i]=String.valueOf(b[i]);
??????????????????? d1[i]=String.valueOf(d[i]);
?????????????? ?????j1[i]=new JLabel(a1[i]);
??????????????????? j2[i]=new JLabel("+");
??????????????????? j3[i]=new JLabel(b1[i]);
??????????????????? j4[i]=new JLabel("=");
???????????????????
??????????????? }
??????????????? if(e[i]==2)
??????????????? {
??????????????????? int x;
??????????????????? a[i]=r1[i].nextInt(c3-c2)+c2;
??????????????????? b[i]=r2[i].nextInt(c3-c2)+c2;
??????????????????? if(b[i]>a[i])
??????????????????? {
??????????????????????? x=b[i];
??????????????????????? b[i]=a[i];
????? ??????????????????a[i]=x;
???????????????????????
??????????????????? }
??????????????????? d[i]=a[i]-b[i];
??????????????????? a1[i]=String.valueOf(a[i]);
??????????????????? b1[i]=String.valueOf(b[i]);
??????????????????? d1[i]=String.valueOf(d[i]);
?? ?????????????????j1[i]=new JLabel(a1[i]);
??????????????????? j2[i]=new JLabel("-");
??????????????????? j3[i]=new JLabel(b1[i]);
??????????????????? j4[i]=new JLabel("=");
???????????????????
??????????????? }
??????????? }
???????????
??????????? if(e[i]==3)
??????????? {
??????????????? a[i]=r1[i].nextInt(c3-c2)+c2;
??????????????? b[i]=r2[i].nextInt(c3-c2)+c2;
??????????????? d[i]=a[i]*b[i];
??????????????? a1[i]=String.valueOf(a[i]);
??????????????? b1[i]=String.valueOf(b[i]);
??????????????? d1[i]=String.valueOf(d[i]);
??????????????? j1[i]=new JLabel(a1[i]);
??????????????? j2[i]=new JLabel("X");
??????????????? j3[i]=new JLabel(b1[i]);
??????????????? j4[i]=new JLabel("=");
???????????????
??????????? }
??????????? if(f4==false)
??????????? {
??????????? if(e[i]==4)
??????????? {
??????????????? d[i]=r1[i].nextInt(c3-c2)+c2;
??????????????? b[i]=r2[i].nextInt(c3-c2)+c2;
??????????????? a[i]=b[i]*d[i];
??????????????? while(d[i]==0||b[i]==0)
??????????????? {
???????????????? ???r1[i]=new Random();
??????????????????? r2[i]=new Random();
??????????????????? d[i]=r1[i].nextInt(20);
??????????????????? b[i]=r2[i].nextInt(20);
??????????????????? a[i]=b[i]*d[i];
??????????????? }
???????????????
??????????????? a1[i]=String.valueOf(a[i]);
??????????????? b1[i]=String.valueOf(b[i]);
??????????????? d1[i]=String.valueOf(d[i]);
??????????????? j1[i]=new JLabel(a1[i]);
??????????????? j2[i]=new JLabel("/");
??????????????? j3[i]=new JLabel(b1[i]);
??????????????? j4[i]=new JLabel("=");
???????????????
???????????????
??????????? }
??????????? }
??????????? else
??????????? {
??????????????? if(e[i]==4)
??????????????? {
??????????????????? j7[i]=new JLabel("余");
??????????????????? a[i]=r1[i].nextInt(c3-c2)+c2;
??????????????????? b[i]=r2[i].nextInt(c3-c2)+c2;
??????????????????? while(a[i]==0||b[i]==0)
??????????????????? {
??????????????????????? r1[i]=new Random();
??????????????????????? r2[i]=new Random();
??????????????????????? d[i]=r1[i].nextInt(20);
??????????????????????? b[i]=r2[i].nextInt(20);
??????????????????????? a[i]=b[i]*d[i];
??????????????????? }
???????????????????
??????????????????? a1[i]=String.valueOf(a[i]);
??????????????????? b1[i]=String.valueOf(b[i]);
???????????????? ???d1[i]=String.valueOf(d[i]);
??????????????????? j1[i]=new JLabel(a1[i]);
??????????????????? j2[i]=new JLabel("/");
??????????????????? j3[i]=new JLabel(b1[i]);
??????????????????? j4[i]=new JLabel("=");
???????????????????
???????????????????
?
??????????????? }
??????????? }
??????????? if(e[i]==4&&f4==true)
??????????? {
??????????? j1[i].setBounds(50+j*42, 50+(k/2)*32, 40, 30);
??????????? j++;
??????????? j2[i].setBounds(50+j*42, 50+(k/2)*32, 40, 30);
??????????? j++;
??????????? j3[i].setBounds(50+j*42, 50+(k/2)*32, 40, 30);
??????????? j++;
??????????? j4[i].setBounds(50+j*42, 50+(k/2)*32, 40, 30);
??????????? j++;
??????????? j5[i].setBounds(50+j*42, 50+(k/2)*32, 40, 30);
??????????? j++;
??????????? j7[i].setBounds(65+j*42,50+(k/2)*32, 40, 30);
??????????? j++;
??????????? j6[i].setBounds(50+j*42,50+(k/2)*32, 40, 30);
??????????? j++;
??????????? j++;
??????????? j++;
??????????? k++;
??????????? if(k!=0&&k%2==0)
??????????? {
??????????????? j=0;
??????????? }
??????????? c.add(j1[i]);
??????????? c.add(j2[i]);
??????????? c.add(j3[i]);
??????????? c.add(j4[i]);
??????????? c.add(j5[i]);
??????????? c.add(j6[i]);
??????????? c.add(j7[i]);
??????????? }
??????????? else
??????????? {
??????????????? j1[i].setBounds(50+j*42, 50+(k/2)*32, 40, 30);
??????????????? j++;
??????????????? j2[i].setBounds(50+j*42, 50+(k/2)*32, 40, 30);
??????????????? j++;
??????????????? j3[i].setBounds(50+j*42, 50+(k/2)*32, 40, 30);
??????????????? j++;
??????????????? j4[i].setBounds(50+j*42, 50+(k/2)*32, 40, 30);
??????????????? j++;
??????????????? j5[i].setBounds(50+j*42, 50+(k/2)*32, 40, 30);
??????????????? j++;
??????????????? j++;
??????????????? j++;
??????????????? j++;
??????????????? j++;
??????????????? k++;
??????????????? if(k!=0&&k%2==0)
???????? ???????{
??????????????????? j=0;
??????????????? }
??????????????? c.add(j1[i]);
??????????????? c.add(j2[i]);
??????????????? c.add(j3[i]);
??????????????? c.add(j4[i]);
??????????????? c.add(j5[i]);
??????????? }
??????? }
??????? jf.setVisible(true);
??????? jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
??? }
?
}
?
設計思路:
1.建立一個frame面板,通過添加復選框來選擇是否有乘除法,是否有余數,是否有括號以及是否有負數
2.通過添加文本框來控制計算范圍和題目數目。
3.當點擊確定按鈕,當前窗口關閉,數據提交,并新建一個frame窗口,frame窗口大小根據題目數目而定。
4.通過random產生隨機數來控制計算題目以及加減乘除(0-3分別代表加減乘除,若沒有選擇有乘除,則產生0或1,從而將乘除運算排除掉)
5.根據是否選擇有余數來控制除法。若沒有余數,則將生成的兩個隨機數之積作為被除數,第二個隨機數作為除數。
6.若選擇有負數,加減不做處理,否則比較減數與被減數大小。若減數大于被減數,則將二者交換。
7.通過建立JLabel數組來向窗口輸出題目,題目坐標按照題目數累加
8.通過建立文本框來填入答案
9.括號和檢驗重復沒做。
轉載于:https://www.cnblogs.com/LJT666/p/5528551.html
總結
以上是生活随笔為你收集整理的软件工程个人作业(2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Debian Linux下的Python
- 下一篇: 数据结构与算法09 之图