100以内习题练习器

package com.zd.exercise.view.entrance;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.zd.exercise.controller.ExerciseController;
import com.zd.exercise.view.ChoosePanel;
import com.zd.exercise.view.ProgressPanel;
import com.zd.exercise.view.ResultPanel;
import com.zd.exercise.view.WorkspacePanel;

public class ExerciseFrame extends JFrame {     //入口类

	
	private static final long serialVersionUID = 3285522428578124725L;
	
	public JPanel contentPane;    
	private ChoosePanel choosePanel;
	private ProgressPanel progressPanel;
	private WorkspacePanel workPanel;
    private ResultPanel resultPanel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					
					ExerciseFrame frame = new ExerciseFrame();
					frame.setVisible(true);
					frame.setResizable(false);
					frame.start();
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ExerciseFrame() {
		
		init();
		
		choosePanel=new ChoosePanel();	
		contentPane.add(choosePanel);
				     
		progressPanel=new ProgressPanel();
		contentPane.add(progressPanel);
		
		workPanel=new WorkspacePanel();
		contentPane.add(workPanel);
		
		resultPanel=new ResultPanel();
		contentPane.add(resultPanel);			

	}

	private void init() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 530, 468);
		setTitle("100以内运算题练习程序");
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
	}
	
	public void start(){
		new ExerciseController(this);
	}

	public ChoosePanel getChoosePanel() {
		return choosePanel;
	}

	public ProgressPanel getProgressPanel() {
		return progressPanel;
	}



	public WorkspacePanel getWorkspacePanel() {
		return workPanel;
	}

	
	public ResultPanel getResultPanel() {
		return resultPanel;
	}

	
	
}
//选择题目数量,开始类
package com.zd.exercise.view;

import java.awt.Color;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.TitledBorder;

public class ChoosePanel extends JPanel  {
	
	private static final long serialVersionUID = 6534856193743477934L;
	
	private JRadioButton radio_10;
	private JRadioButton radio_20;
	private JRadioButton radio_50;
	
	private ButtonGroup  buttonGroup=new ButtonGroup();
		
	private JButton startButton;
	private JButton exitButton;

	public ChoosePanel() {	
		
		this.init()		
			.initRadio_10()		
			.initRadio_20()		
			.initRadio_50()		
			.initStartBtn()
			.initExitBtn();
		
	}


	private void initExitBtn() {
		exitButton = new JButton("退出");
		exitButton.setFont(new Font("宋体", Font.BOLD, 12));
		exitButton.setBounds(389, 21, 89, 24);
		add(exitButton);
		
	}


	private ChoosePanel initStartBtn() {
		startButton = new JButton("开始");
		startButton.setFont(new Font("宋体", Font.BOLD, 12));
		startButton.setBounds(290, 21, 89, 24);
		this.add(startButton);
		return this;
	}




	private ChoosePanel initRadio_50() {
	    radio_50 = new JRadioButton("50题");
		radio_50.setBounds(174, 23, 76, 23);
		this.add(radio_50);
		buttonGroup.add(radio_50);
		return this;
	}


	private ChoosePanel initRadio_20() {
		radio_20 = new JRadioButton("20题");
		radio_20.setBounds(84, 23, 76, 23);
		this.add(radio_20);
		buttonGroup.add(radio_20);
		return this;
	}


	private ChoosePanel initRadio_10() {
		radio_10 = new JRadioButton("10题");
		radio_10.setBounds(6, 23, 76, 23);
		this.add(radio_10);
		buttonGroup.add(radio_10);
		return this;
	}


	private ChoosePanel init() {
		this.setBorder(BorderFactory.createTitledBorder(null, "选择或者输入题目数量", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
		this.setBounds(10, 11, 494, 54);
		this.setLayout(null);
		return this;
	}





	public JButton getStartButton() {
		return startButton;
	}


	public ButtonGroup getButtonGroup() {
		return buttonGroup;
	}


	public void setButtonGroup(ButtonGroup buttonGroup) {
		this.buttonGroup = buttonGroup;
	}


	
	public void refresh() {
		buttonGroup.clearSelection();
		
	}


	public JButton getExitButton() {
		return exitButton;
	}

}
//进度类
package com.zd.exercise.view;

import java.awt.Color;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

public class ProgressPanel extends JPanel {
	
	private static final long serialVersionUID = 733178387581655720L;
	
	private JLabel timeUsedLab;
	private JLabel timeUsedValue;
	private JLabel unitSecondLab;
	
	private JLabel rightSubjectLab;
	private JLabel rightSubjectValue;
	
	private JLabel unitSubject;
	private JLabel unitSubject_1;
	
	private JLabel remainderLab;
	private JLabel remainderValue;
	
	public ProgressPanel(){
					
		this.init()
		    .initTimeUsedLab()		
		    .initITimeUsedValue()	
			.initUnitSecondLab()	
			.initRightSubjectLab()
			.initRightSubjectValue()	
			.initUnitSubject()	
			.initRemainderTab()		
			.initRemainderValue()	
			.initUnitSubject_1();
		
	}

	private ProgressPanel initUnitSubject_1() {
		unitSubject_1 = new JLabel("道");
		 unitSubject_1.setFont(new Font("宋体", Font.BOLD, 13));
		 unitSubject_1.setBounds(452, 26, 32, 22);
		 this.add(unitSubject_1);
		 return this;
	}

	private ProgressPanel initRemainderValue() {
		remainderValue = new JLabel("0");
		remainderValue.setFont(new Font("宋体", Font.BOLD, 13));
		remainderValue.setBounds(417, 26, 36, 22);
		this.add(remainderValue);
		return this;
	}

	private ProgressPanel initRemainderTab() {
		remainderLab = new JLabel("还剩");
		remainderLab.setFont(new Font("宋体", Font.BOLD, 13));
		remainderLab.setBounds(358, 26, 49, 22);
		this.add(remainderLab);
		return this;
	}

	private ProgressPanel initUnitSubject() {
		unitSubject= new JLabel("道");
		unitSubject.setFont(new Font("宋体", Font.BOLD, 13));
		unitSubject.setBounds(301, 26, 42, 22);
		this.add(unitSubject);
		return this;
	}

	private ProgressPanel initRightSubjectValue() {

		rightSubjectValue = new JLabel("0");
		rightSubjectValue.setFont(new Font("宋体", Font.BOLD, 13));
		rightSubjectValue.setBounds(263, 26, 36, 22);
		this.add(rightSubjectValue);
		return this;
		
	}

	private ProgressPanel initRightSubjectLab() {
		rightSubjectLab = new JLabel("已做对");
		rightSubjectLab.setFont(new Font("宋体", Font.BOLD, 13));
		rightSubjectLab.setBounds(200, 26, 64, 22);
		this.add(rightSubjectLab);
		return this;
	}

	private ProgressPanel initUnitSecondLab() {
		unitSecondLab = new JLabel("秒");
		unitSecondLab.setFont(new Font("宋体", Font.BOLD, 13));
		unitSecondLab.setBounds(142, 26, 42, 22);
		this.add(unitSecondLab);
		return this;
	}

	private ProgressPanel initITimeUsedValue() {
		timeUsedValue = new JLabel("0");
	    timeUsedValue.setFont(new Font("宋体", Font.BOLD, 13));
	    timeUsedValue.setBounds(94, 26, 36, 22);
		this.add(timeUsedValue);
		return this;
	}

	private ProgressPanel initTimeUsedLab() {
		timeUsedLab = new JLabel("已用时");
		timeUsedLab.setFont(new Font("宋体", Font.BOLD, 13));
		timeUsedLab.setBounds(10, 26, 64, 22);
		this.add(timeUsedLab);
		return this;
	}
	
	public ProgressPanel init(){
		
		this.setBorder(BorderFactory.createTitledBorder(null, "进度", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
		this.setBounds(10, 64, 494, 79);
		this.setLayout(null);
		this.setSize(494, 66);
		this.setLocation(10, 76);
		
		return this;
		
	}

	public JLabel getTimeUsedValue() {
		return timeUsedValue;
	}

	public void setTimeUsedValue(JLabel timeUsedValue) {
		this.timeUsedValue = timeUsedValue;
	}

	public JLabel getRightSubjectValue() {
		return rightSubjectValue;
	}

	public void setRightSubjectValue(JLabel rightSubjectValue) {
		this.rightSubjectValue = rightSubjectValue;
	}

	public JLabel getRemainderValue() {
		return remainderValue;
	}

	public void setRemainderValue(JLabel remainderValue) {
		this.remainderValue = remainderValue;
	}

	
	public void refresh() {
		timeUsedValue.setText("0");
		rightSubjectValue.setText("0");
		remainderValue.setText("0");
	}
	
	

}
//结果类
package com.zd.exercise.view;

import java.awt.Color;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

public class ResultPanel extends JPanel {	

	private static final long serialVersionUID = 1L;
	
	private JLabel rateLab;
	private JLabel rateValueLab;
	private JLabel evaluateLab;	
	private JLabel evaluateValueLab;

	public ResultPanel(){
		
		this.init()	
		    .initRateLab()	
		    .initRateValueLab()		
		    .initEvaluateLab()	
		    .initEvaluateValueLab();
	}

	private ResultPanel initEvaluateValueLab() {
		evaluateValueLab = new JLabel("");
		evaluateValueLab.setFont(new Font("宋体", Font.BOLD, 13));
		evaluateValueLab.setBounds(253, 46, 231, 14);
		this.add(evaluateValueLab);
		return this;
	}

	private ResultPanel initEvaluateLab() {
		evaluateLab = new JLabel("评价:");
		evaluateLab.setFont(new Font("宋体", Font.BOLD, 13));
		evaluateLab.setBounds(158, 46, 91, 14);
		this.add(evaluateLab);
		return this;
	}

	private ResultPanel initRateValueLab() {
		rateValueLab = new JLabel("");
		rateValueLab.setFont(new Font("宋体", Font.BOLD, 13));
		rateValueLab.setBounds(74, 39, 74, 27);
		this.add(rateValueLab);
		return this;
	}

	private ResultPanel initRateLab() {
		rateLab = new JLabel("分数:");
		rateLab.setFont(new Font("宋体", Font.BOLD, 13));
		rateLab.setBounds(10, 33, 74, 39);
		this.add(rateLab);
		return this;
	}

	private ResultPanel init() {
		this.setBorder(BorderFactory.createTitledBorder(null, "结果", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
		this.setBounds(10, 336, 494, 83);	
		this.setLayout(null);
		return this;
	}

	public JLabel getRateValueLab() {
		return rateValueLab;
	}



	public JLabel getEvaluateValueLab() {
		return evaluateValueLab;
	}

	public void refresh() {
		rateValueLab.setText("");
		evaluateValueLab.setText("");	
	}


}
//练习类
package com.zd.exercise.view;

import java.awt.Color;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;

public class WorkspacePanel extends JPanel {

	private static final long serialVersionUID = -8237247809666273864L;
	
	private JLabel leftNumLab;
	private JLabel operatorLab;
    private JLabel rightNumLab;
    private JLabel equalMarkLab;
    private JLabel questionMarkLab;
    private JLabel promptLab;
    
    private JTextField resultText;

    private JButton commitBtn;
    private JButton skipBtn;

	public WorkspacePanel() {
		
	    this.init()	
			.initLeftNumTab()
			.initOperatorLab()
			.initRightNumLab()	
			.initEqualMarkLab()		
			.initQuestionMarkLab()
			.initPromptLab()	
			.initResultText()
			.initCommitBtn()
			.initSkipBtn();
		
	}

	private WorkspacePanel initCommitBtn() {
		commitBtn = new JButton("提交");
		commitBtn.setFont(new Font("宋体", Font.BOLD, 13));
		commitBtn.setBounds(261, 113, 86, 35);
		commitBtn.setEnabled(false);
		this.add(commitBtn);
		
		
		return this;
	}

	private void initSkipBtn() {
		skipBtn = new JButton("跳过");
		skipBtn.setFont(new Font("宋体", Font.BOLD, 13));
		skipBtn.setEnabled(false);
		skipBtn.setBounds(385, 113, 86, 35);
		 this.add(skipBtn);
	}

	private WorkspacePanel initResultText() {
		resultText = new JTextField();
		resultText.setBounds(143, 114, 86, 35);		
		resultText.setColumns(10);
		this.add(resultText);
		return this;
	}

	private WorkspacePanel initPromptLab() {
		promptLab = new JLabel("这里输入答案:");
		promptLab.setFont(new Font("宋体", Font.BOLD, 13));
		promptLab.setBounds(39, 113, 94, 35);
		this.add(promptLab);
		return this;
	}

	private WorkspacePanel initQuestionMarkLab() {
		questionMarkLab = new JLabel("?");
		questionMarkLab.setHorizontalAlignment(SwingConstants.CENTER);
		questionMarkLab.setFont(new Font("Tahoma", Font.BOLD, 18));
		questionMarkLab.setBackground(Color.WHITE);
		questionMarkLab.setBounds(388, 31, 62, 54);
		this.add(questionMarkLab);
		return this;
	}

	private WorkspacePanel initEqualMarkLab() {
		equalMarkLab = new JLabel("=");
		equalMarkLab.setHorizontalAlignment(SwingConstants.CENTER);
		equalMarkLab.setFont(new Font("Tahoma", Font.BOLD, 18));
		equalMarkLab.setBackground(Color.WHITE);
		equalMarkLab.setBounds(295, 30, 62, 54);
		this.add(equalMarkLab);
		return this;
	}

	private WorkspacePanel initRightNumLab() {
		rightNumLab = new JLabel(" ");
		rightNumLab.setHorizontalAlignment(SwingConstants.CENTER);
		rightNumLab.setFont(new Font("Tahoma", Font.BOLD, 13));
		rightNumLab.setBackground(Color.WHITE);
		rightNumLab.setBounds(191, 31, 94, 54);
		this.add(rightNumLab);
		return this;
	}

	private WorkspacePanel initOperatorLab() {
		operatorLab = new JLabel("");
		operatorLab.setHorizontalAlignment(SwingConstants.CENTER);
		operatorLab.setFont(new Font("宋体", Font.BOLD, 18));
		operatorLab.setBackground(Color.WHITE);
		operatorLab.setBounds(132, 30, 68, 54);
		this.add(operatorLab);
		return this;
	}

	private WorkspacePanel initLeftNumTab() {
		leftNumLab = new JLabel("");
		leftNumLab.setHorizontalAlignment(SwingConstants.CENTER);
		leftNumLab.setBackground(Color.WHITE);
		leftNumLab.setFont(new Font("宋体", Font.BOLD, 13));
		leftNumLab.setBounds(27, 31, 94, 54);
		this.add(leftNumLab);
		return this;
	}

	private WorkspacePanel init() {
		this.setBorder(BorderFactory.createTitledBorder(null, "练习区", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
		this.setBounds(10, 117, 494, 196);	
		this.setLayout(null);
		this.setSize(494, 172);
		this.setLocation(10, 153);
		return this;
	}

	public JLabel getLeftNumLab() {
		return leftNumLab;
	}


	public JLabel getRightNumLab() {
		return rightNumLab;
	}


	public JTextField getResultText() {
		return resultText;
	}


	public JButton getCommitBtn() {
		return commitBtn;
	}


	public JLabel getOperatorLab() {
		return operatorLab;
	}

	

	public JButton getSkipBtn() {
		return skipBtn;
	}


	
	public void refresh() {
		leftNumLab.setText("");
		operatorLab.setText("");
		rightNumLab.setText("");
		resultText.setText("");
		commitBtn.setEnabled(false);
		skipBtn.setEnabled(false);
	}
}
//逻辑控制类
package com.zd.exercise.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.util.Enumeration;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.AbstractButton;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;

import com.zd.exercise.view.ChoosePanel;
import com.zd.exercise.view.ProgressPanel;
import com.zd.exercise.view.ResultPanel;
import com.zd.exercise.view.WorkspacePanel;
import com.zd.exercise.view.entrance.ExerciseFrame;

public class ExerciseController {

    private ExerciseFrame exerciseFrame;
	private String selected;   //选中的习题数
	
	private static String[] OPERATORS = { "+", "-", "*", "/" };
	
	private Random rand = new Random(); //产生随机数

	private Timer timer;                //定时器

	private double currentResult = 0d;  //当前习题的计算结果

	public ExerciseController(ExerciseFrame exerciseFrame) {
		this.exerciseFrame=exerciseFrame;
		init();
	}

	private void init() {

		addRadioListener();
		addStartBtnListener();
		addCommitBtnListener();
		addSkipBtnListener();
		addExitBtnListener();
	}
	
	/**
	 * 退出按钮
	 * */
	private void addExitBtnListener() {
		exerciseFrame.getChoosePanel().getExitButton().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(exerciseFrame,"程序即将退出!","提示",JOptionPane.WARNING_MESSAGE);
				System.exit(0);	
			}

		});
	}

     /**
      * 跳过当前习题按钮
      * */
	private void addSkipBtnListener() {

		getWorkspacePanel().getSkipBtn().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				if (checkRemainder()) {return;} //每次跳过之前先检查剩余题数,如果为0,说明本轮练习结束
							
				updateRemainderValue(String.valueOf(getRemainder() - 1));  //更新剩余题数
				generateExercise();                                  //生成新的题目
				getWorkspacePanel().getResultText().setText("");      //刷新结果输入框

			}

		});

	}
    
	/**
	 * 提交当前习题答案按钮
	 * **/
	private void addCommitBtnListener() {
		getWorkspacePanel().getCommitBtn().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
                
				if(checkRemainder()){return;}//每次跳过之前先检查剩余题数,如果为0,说明本轮练习结束
				
				String customInput = getWorkspacePanel().getResultText().getText(); //获取输入的答案
				validateInputValue(customInput);  //校验输入答案的正确性
			
				updateRemainderValue(String.valueOf(getRemainder() - 1)); //更新剩余题数
				updateRightSubjectValue(customInput);  //更新做对题数
				
				generateExercise(); //生成新的题目

				getWorkspacePanel().getResultText().setText(""); //刷新答案输入框
				
			}

			private void validateInputValue(String customInput) {
				if (null == customInput || "".equals(customInput)) { //输入答案不能为空
					JOptionPane.showMessageDialog(exerciseFrame, "请输入答案!","提示", JOptionPane.WARNING_MESSAGE);
					return;
				}

				if (!customInput.matches("^[0-9]+$")) { //输入的答案必须是数字
					JOptionPane.showMessageDialog(exerciseFrame, "只能输入数字!","提示", JOptionPane.WARNING_MESSAGE);
					return;
				}
			}

		});

	}
	
    /***
     * 获取剩余题数
     * */
	private int getRemainder() {
		String remainderStr = getProgressPanel().getRemainderValue().getText();
		int remainder = Integer.parseInt(remainderStr);
		return remainder;
	}
    
	/**
	 * 开始按钮
	 * **/
	private void addStartBtnListener() {

		getChoosePanel().getStartButton().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				if (selected == null || "".equals(selected)) {				
						JOptionPane.showMessageDialog(exerciseFrame,"请选择习题数量后再点击开始!", "提示",JOptionPane.WARNING_MESSAGE);
						return;					
				} 

				getResultPanel().refresh();
				
				selected = selected.substring(0, 2);	 		

				getWorkspacePanel().getCommitBtn().setEnabled(true);
				getWorkspacePanel().getSkipBtn().setEnabled(true);
				getChoosePanel().getStartButton().setEnabled(false);
                
				startTimer(); //开始计时

				int remainder = Integer.parseInt(selected);      
				updateRemainderValue(String.valueOf(remainder));  //更新剩余题数

				generateExercise(); //生成一道题
			}

		});

	}

	/**
	 * 选择题目数量的单选按钮
	 * **/
	private void addRadioListener() {

		Enumeration<AbstractButton> radiobtns = getChoosePanel().getButtonGroup().getElements();
		while (radiobtns.hasMoreElements()) {
			AbstractButton radio = radiobtns.nextElement();
			radio.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					JRadioButton temp = (JRadioButton) e.getSource();
					if (temp.isSelected()) {
						selected = temp.getText();
					}
				}

			});
		}

	}
  
	/**
	 * 点击开始按钮时,开始计时 
	 * **/
	private void startTimer() {  
		timer = new Timer();
		timer.schedule(new TimerTask() {
			public void run() {
				int second = Integer.parseInt(getProgressPanel().getTimeUsedValue().getText());
				second++;
				getProgressPanel().getTimeUsedValue().setText(String.valueOf(second));
			}
		}, 0, 1000l);

	}
    
	/**
	 * 本轮题目做完后结束计时
	 * */
	private void stopTimer() {  //停止计时
		if (null != timer) {
			timer.cancel();
		}
	}
    
	
	/**
	 * 生成一道题
	 * **/
	private void generateExercise() {
		
		String operator = generateOperator();
		double result = 0;

		while (true) {

			int leftNum = rand.nextInt(100);
			int rightNum = rand.nextInt(100)+1;
			

			if (operator.equals(OPERATORS[0])) {
				result = leftNum + rightNum;
			}

			if (operator.equals(OPERATORS[1])) {
				result = leftNum - rightNum;
			}

			if (operator.equals(OPERATORS[2])) {
				result = leftNum * rightNum;
			}

			if (operator.equals(OPERATORS[3])) {
				
				
					
				while(leftNum % rightNum != 0 ){    //只生成整除
										
					leftNum = rand.nextInt(100);
					rightNum = rand.nextInt(100)+1;
			
				}
								
			
				result = leftNum / rightNum;
			}

			if (result <= 100 && result >= 0) {      //控制计算结果在100以内   
				currentResult = result;

				getWorkspacePanel().getLeftNumLab().setText(String.valueOf(leftNum));
				getWorkspacePanel().getRightNumLab().setText(String.valueOf(rightNum));
				getWorkspacePanel().getOperatorLab().setText(operator);
				
				break;
			}

		}

	}
    
	/**
	 * 随机产生运算符号
	 * */
	private String generateOperator() {
		return OPERATORS[rand.nextInt(4)];
	}
    
	/**
	 * 更新剩余题数
	 * */
	private void updateRemainderValue(String valueStr) {
		getProgressPanel().getRemainderValue().setText(valueStr);
	}
    
	/**
	 * 检查剩余题数
	 * */
	private boolean checkRemainder() {

		if (getRemainder() == 0) {  //如果剩余题数为0,说明做完了
			stopTimer();  //结束计时
			JOptionPane.showMessageDialog(exerciseFrame, "恭喜你,做完了全部习题!", "提示",JOptionPane.WARNING_MESSAGE);
			generateResult();//生成结果
			refresh(); //刷新页面
			return true;
		}

		return false;
	}
    
	/**
	 * 生成练习结果
	 * **/
	private void generateResult() {
	  
		double _selected=Double.parseDouble(selected);
		String right=getProgressPanel().getRightSubjectValue().getText();
		double _right=Double.parseDouble(right);
		double rate=getRate(_selected,_right);
		String evaluate=getEvaluate(rate);
		getResultPanel().getRateValueLab().setText(String.valueOf(rate));
		getResultPanel().getEvaluateValueLab().setText(evaluate);
	}
    
	/**
	 * 开始新一轮练习时,刷新页面
	 * */
	private void refresh() {
		
		currentResult = 0d;
		selected=null;
		getChoosePanel().getStartButton().setEnabled(true);		
		getChoosePanel().refresh();
		getProgressPanel().refresh();
		getWorkspacePanel().refresh();
	

	}
    
	/**
	 * 更新做对题数
	 * */
	private void updateRightSubjectValue(String customInput) {

		if (currentResult == Double.parseDouble(customInput)) {
			String rightCountStr = getProgressPanel().getRightSubjectValue().getText();
			int rightCount = Integer.parseInt(rightCountStr);
			rightCount++;
			getProgressPanel().getRightSubjectValue().setText(String.valueOf(rightCount));
		}

	}
	
	private ChoosePanel getChoosePanel(){
		return exerciseFrame.getChoosePanel();
	}
	
	private ProgressPanel getProgressPanel(){
		return exerciseFrame.getProgressPanel();
	}
	
	private WorkspacePanel getWorkspacePanel(){
		return exerciseFrame.getWorkspacePanel();
	}
	
	private ResultPanel getResultPanel(){
		return exerciseFrame.getResultPanel();
	}
	
	/**
	 * 获取正确率
	 * */
	private double getRate(double m,double n){		    
	       return (divide(m,n)*100);		
	}
	
	/**
	 * 获取评价
	 * **/
	private  String getEvaluate(double rate){
		
		if(rate<60){return "正确率偏低,还需要多加练习!";}
		
		if(rate>=60 && rate<80){return "正确率中等,还需多加练习!";}
		
		if(rate>=80 && rate<90){return "正确率较高,棒棒哒!";}
		
		if(rate >=90){return "百分之百正确!";}
		
		return "";
	}
	
	/**
	 * 精度除法
	 * */
	private double divide(double m ,double n){
				 
	     BigDecimal b1 = new BigDecimal(m);
         BigDecimal b2 = new BigDecimal(n);
         return b2.divide(b1).doubleValue();
			
	}

}

结构:




运行效果图: 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值