小学数学减法出题+批改程序



import javax.swing.JOptionPane;

public class SubtrationQuizLoop {
	public static void main(String[] args) {
		final int NUMBER_OF_QUESTIONS = 5;                            //出5个减法题
		int correctCount = 0;
		int count = 0;
		long startTime = System.currentTimeMillis();                      //计时器
		String output = " ";
		
		
		
		while(count < NUMBER_OF_QUESTIONS) {                //随机出题
			int number1 = (int)(Math.random()*10);
			int number2 = (int)(Math.random()*10);
			
			if(number1 < number2) {
				int temp=number1;
				number1 = number2;
				number2 = temp;
			}
	
			
			String answerString =JOptionPane.showInputDialog("What is "+number1+ " - "+number2+" ? ");
			
			int answer = Integer.parseInt(answerString);              //将字符串answerString转化为整型变量answer
			
			if(number1 - number2 == answer) {
				JOptionPane.showMessageDialog(null,"You are correct");
				correctCount++;
				
			}
			else 
				JOptionPane.showMessageDialog(null,number1 +" - "+number2+" should be" + (number1-number2)," You are wrong! ",
						JOptionPane.INFORMATION_MESSAGE);
			count++;
			
			output += "\n" +number1 +" - "+ number2 +" = " +answer +((number1 - number2 == answer) ? "correct" : "wrong");      //做结果总览输出
			    
		}
		
		long endTime = System.currentTimeMillis();
		long testTime = endTime-startTime;
		
		JOptionPane.showMessageDialog(null,"Correct count is " + correctCount +"\n Test time is "+testTime/1000 +"seconds\n"+output);
		
	}

}

特意写了JOptionPane的 对话框形式,觉得非常的合适。注意JOptionPane有两种输入方式,以JOptionPane.showMessageDialog为例,一种是JOptionPane.showMessageDialog(null,x)
另一种是JOptionPane.showMessageDialog(null,x,y,JOptionPane.INFORMATION_MASSAGE).上面的代码用到了两种写法,请自行观察输出的对话框有什么不同。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值