码迷,mamicode.com
首页 > 其他好文 > 详细

作业5.2 5.3

时间:2015-05-08 14:31:47      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

package fiv;
public class calc {
    int na[]=new int[5];
    String nc[]=new String[5];
    int ncn=0,nm;
    int re=0;
    calc(){
        
    }
    calc(int[][] a,String[][] c,int m,int cn){
        for(int i=0;i<cn+1;i++)
            na[i]=a[m][i];
        for(int i=0;i<cn;i++)
            nc[i]=c[m][i];
        ncn=cn;
        nm=m;
    }
    int add(int a,int b){
        return a+b;
    } 
    int minius(int a,int b){
        return a-b;
    }
    int mult(int a,int b){
        return a*b;
    }
    int divi(int a,int b){
        return a/b;
    }
    void to(){
        for(int i=0;i<ncn+1;i++)
            System.out.println("a"+i+"="+na[i]);
    }
    void setting(){
        int rn=0,r=0;
        int[] b=new int[5];
        String[] cc=new String[5];
        re=0;
        for(int i=0;i<ncn;i++){
            if(rn==0){
                if(nc[i]=="*"||nc[i]=="/"){
                    if(nc[i]=="*"){
                        b[r]=na[i]*na[i+1];
                        rn=1;
                    }
                    if(nc[i]=="/"){
                        b[r]=na[i]/na[i+1];
                        rn=1;
                    }
                }
                else{
                    if(nc[i]=="+"){
                        b[r]=na[i];
                        cc[r]="+";
                        if(ncn==i+1)
                            b[r+1]=na[i+1];
                        r++;
                        }
                    if(nc[i]=="-"){
                        b[r]=na[i];
                        cc[r]="-";
                        if(ncn==i+1)
                            b[r+1]=na[i+1];
                        r++;
                    }
                }
            }
            else{
                if(nc[i]=="*"||nc[i]=="/"){
                    if(nc[i]=="*"){
                        b[r]=b[r]*na[i+1];
                    }
                    if(nc[i]=="/"){
                        b[r]=b[r]/na[i+1];
                    }
                }
                else{
                    if(nc[i]=="+"){
                        cc[r]="+";
                        r++;
                        b[r]=na[i+1];
                        rn=0;
                        }
                    if(nc[i]=="-"){
                        cc[r]="-";
                        r++;
                        b[r]=na[i+1];
                        rn=0;
                    }
                }
                }
        }
        if(cc[0]=="+"){
            re=b[0]+b[1];
        }
        if(cc[0]=="-"){
            re=b[0]-b[1];
        }
        //System.out.println("a"+"="+b[2]);
        for(int i=1;i<r;i++){
            if(cc[i]=="+"){
                re=re+b[i+1];
                //System.out.println("a"+"="+re);
            }
            if(cc[i]=="-"){
                re=re-b[i+1];
                //System.out.println("a"+"="+re);
            }
        }
        //System.out.println("a"+"="+re);
    }
}
package fiv;
import java.awt.*; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;

import javax.swing.*; 
public  class rrr extends JFrame implements ActionListener{
    JLabel L1=new JLabel("题目:");
    JLabel L2=new JLabel("输入题目个数:"); 
    JLabel L3=new JLabel("输入答案");
    public JLabel L4=new JLabel("        "); 
    JLabel L5=new JLabel("        ");
    JLabel L6=new JLabel("运算符个数:");
    JTextField T1=new JTextField(6);
    JTextField T2=new JTextField("1");
    JTextField T3=new JTextField("1");
    JButton B1=new JButton("开始");
    JButton B2=new JButton("重新开始");
    JButton B3=new JButton("下一题");
    JButton B4=new JButton("提交");
    JPanel p1=new JPanel();
    int n=0,ri,m=0,p,cn=2;
    int a[][]=new int[100][5];
    int b[]=new int[100];
    int d[]=new int[100];
    int e[]=new int[100];
    int f[]=new int[100];
    String s;
    String c[][]=new String[100][4];
    String ch[]={"+","-","*","/"};
    public static void main(String[] args) {
        new rrr();
        
        // TODO Auto-generated method stub
    }
    rrr()
    {
        
            this.setTitle("四则运算");
            this.setSize(400,400);
            this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            this.add(p1); 
            p1.setLayout(null);
            L2.setBounds(50, 20, 90, 20);
            p1.add(L2);
            T2.setBounds(150, 20, 90, 30);
            p1.add(T2);
           L1.setBounds(50, 70, 90, 30);
            p1.add(L1);
            L4.setBounds(150, 70, 170, 30);
            p1.add(L4);
            L3.setBounds(50,120, 90, 30);
            p1.add(L3);
            T1.setBounds(150, 120, 90, 30);
            p1.add(T1);
            L6.setBounds(50,170, 90, 30);
            p1.add(L6);
            T3.setBounds(150, 170, 90, 30);
            p1.add(T3);
            B1.setBounds(50, 220, 90, 30);
            p1.add(B1);
            B1.addActionListener(this);
            B2.setBounds(150, 220, 90, 30);
            p1.add(B2);
            B2.setEnabled(false);
            B2.addActionListener(this);
            B3.setBounds(50,270,90,30);
            p1.add(B3);
            B3.setEnabled(false);
            B3.addActionListener(this);
            B4.setBounds(150, 270,90,30);
            p1.add(B4);
            B4.setEnabled(false);
            B4.addActionListener(this);
            L5.setBounds(50, 320, 220, 30);
            p1.add(L5);
            this.setVisible(true);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//可以关闭窗体     
            B1.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==B1){
            String s1=new String("输入的个数不能大于5或者小于1");
            String s2=new String("运算符数不能大于4或者小于1");
            m=0;
            ri=0;
            n=(int) Double.parseDouble(T2.getText());
            cn=(int) Double.parseDouble(T3.getText());
            if(n>5||n<1||cn>4||cn<1)
            {
                if((n>5&&cn>4)||(n>5&&cn<1)||(n<1&&cn<1)||((n<1&&cn>4))){
                    JOptionPane.showMessageDialog(null,s1+"且"+s2,"注意",JOptionPane.INFORMATION_MESSAGE);
                }
                else if(cn>4||n<2)
                {
                    JOptionPane.showMessageDialog(null,s2,"注意",JOptionPane.INFORMATION_MESSAGE);
                }
                else JOptionPane.showMessageDialog(null,s1,"注意",JOptionPane.INFORMATION_MESSAGE);
            }
            else {begin();
            B1.setEnabled(false);
            B2.setEnabled(true);
            B3.setEnabled(true);
            B4.setEnabled(true);
            T2.setEditable(false);;
            T3.setEditable(false);
            L5.setText(" ");
            }    
            
        }
        if(e.getSource()==B2){
            B1.setEnabled(true);
            B2.setEnabled(false);
            B3.setEnabled(false);
            B4.setEnabled(false);
            T2.setEditable(true);
            T3.setEditable(true);
            T1.setText("");
            T2.setText("1");
            T3.setText("1");
        }
        if(e.getSource()==B3){
                begin();
                T1.setText("");
                B4.setEnabled(true);
                L5.setText(" ");
        }
        if(e.getSource()==B4){
           answer();
            B4.setEnabled(false);
            if(m==n){
                B2.setEnabled(true);
                B3.setEnabled(false);
                B4.setEnabled(false);
                JOptionPane.showMessageDialog(null,"答对了"+ri+"题,答错了"+(n-ri)+"题","成绩",JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }
    void begin(){
        for(int i=0;i<cn+1;i++)
            a[m][i]=(int)(Math.random()*200-100);
        for(int i=0;i<cn;i++)
            c[m][i]=ch[(int)(Math.random()*3)];
        for(int i=1;i<cn;i++){
        while(a[m][i]==0&&c[m][i-1]==ch[3]){
            a[m][i]=(int)(Math.random()*200-100);
        }
       }
        s=Double.toString(a[m][0]);
        for(int j=1;j<cn+1;j++)
        {
        if(a[m][j]<0){
            s=s+c[m][j-1]+"("+Double.toString(a[m][j])+")";
            }
        else {
            s=s+c[m][j-1]+Double.toString(a[m][j]);
        }
        }
        s=s+"=";
        L4.setText(s);
    }
    void answer(){
        calc re=new calc(a,c,m,cn);
        double an=Double.parseDouble(T1.getText());
       re.setting();
       if((re.re)==an){
            L5.setText("答案正确!");
            ri++;
        }
        else{
            L5.setText("答案错误!正确答案是"+re.re);
        }
        m++;
    }
}

 

作业5.2 5.3

标签:

原文地址:http://www.cnblogs.com/poipoi/p/4487523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!