其他分享
首页 > 其他分享> > 四则运算——计应193 第三组 任煜冬

四则运算——计应193 第三组 任煜冬

作者:互联网

1.计划

随机产生十道加减法,从键盘获得结果,与答案对比。

2.开发

suanshi.java

public class suanshi {
  //创建用以保存算式和结果的数组
  static String[] suanshi = new String[10];
  static int[] jieguo = new int[10];
  /**
  * 产生算式并将算是和结果保存到数组中
  */
  suanshi() {
    suanshi();
  }
  void suanshi() {
    for(int i=0;i<10;i++) {
      int a=(int) (Math.random()*100);
      int b=(int) (Math.random()*100);
      if(i<5) {
        suanshi[i]=a+"+"+b+"=";//将算式保存在数组中
        jieguo[i]=a+b;//将结果保存在数组中
      }else {
        if(a>b) {
          suanshi[i]=a+"-"+b+"=";
          jieguo[i]=a-b;
        }else {
          i--;
        }
      }
    }
  }
}

  

jiemain.java

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class jiemian {

  private static final ActionListener ActionListener = null;

  static void createAndShowGUI() {

    // 创建及设置窗口
    JFrame frame = new JFrame("算数题");
    frame.setSize(500, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //设置内容
    JPanel panel = new JPanel();
    frame.add(panel);
    placeComponents(panel);

    // 显示窗口
    frame.setVisible(true);
  }
/**
* 内容
* @param panel
*/
  static void placeComponents(JPanel panel) {

    suanshi j = new suanshi();
    panel.setLayout(null);
    for(int i=0; i<5;i++) {
      JLabel userLabel = new JLabel(i+1+"、 "+j.suanshi[i]);
      userLabel.setBounds(30,i*50,100,50);
      panel.add(userLabel);
    }
    for(int i=5; i<10;i++) {
      JLabel Label = new JLabel(i+1+"、 "+j.suanshi[i]);
      Label.setBounds(300,(i-5)*50,100,50);
      panel.add(Label);
    }
    JLabel Label1 = new JLabel();
    Label1.setBounds(150,300,300,50);
    Label1.setFont(new Font(null, Font.PLAIN, 20));
    panel.add(Label1);

    JTextField text1 = new JTextField(3);
    JTextField text2 = new JTextField(3);
    JTextField text3 = new JTextField(3);
    JTextField text4 = new JTextField(3);

    JTextField text5 = new JTextField(3);    JTextField text6 = new JTextField(3);

    JTextField text7 = new JTextField(3);
    JTextField text8 = new JTextField(3);
    JTextField text9 = new JTextField(3);
    JTextField text10 = new JTextField(3);

    text1.setBounds(100,15,100,20);
    text2.setBounds(100,65,100,20);
    text3.setBounds(100,115,100,20);
    text4.setBounds(100,165,100,20);
    text5.setBounds(100,215,100,20);
    text6.setBounds(365,15,100,20);
    text7.setBounds(365,65,100,20);
    text8.setBounds(365,115,100,20);
    text9.setBounds(365,165,100,20);
    text10.setBounds(370,215,100,20);

    panel.add(text1);
    panel.add(text2);
    panel.add(text3);
    panel.add(text4);
    panel.add(text5);
    panel.add(text6);
    panel.add(text7);
    panel.add(text8);
    panel.add(text9);
    panel.add(text10);

    JButton btn = new JButton("提交");
    btn.setBounds(200, 270, 80, 25);
    panel.add(btn);

    btn.addActionListener( new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        String[] huida ={text1.getText(),text2.getText(),text3.getText(),text4.getText(),text5.getText(),text6.getText(),
            text7.getText(),text8.getText(),text9.getText(),text10.getText()};         List<String> list = new ArrayList<>();         for(int i=0;i<10;i++) {           if(huida[i].equals("")) {             list.add(Integer.toString(i+1));           }else {             try {               if(Integer.parseInt(huida[i])==j.jieguo[i]);               else {                 list.add(Integer.toString(i+1));               }             }catch(Exception e1){               list.add(Integer.toString(i+1));             }           }         }         int count = list.size();         if(count==0){           Label1.setText("恭喜你全对了");         }else {           String cuo="";           for(int i = 0; i < count; i++) {             if(cuo=="") {               cuo = cuo + list.get(i);             }else {               cuo = cuo +","+ list.get(i);             }           }           Label1.setText("第"+cuo+"题错误");         }       }     });   } }

text.java

public class text {
	public static void main(String[] args) {
		jiemian j=new jiemian();
		j.createAndShowGUI();     
	}
}

3.运行效果

标签:第三组,java,suanshi,任煜冬,static,计应,new,import,panel
来源: https://www.cnblogs.com/-rendong/p/14833370.html