计应193王帅领的小学算法程序
小学数学计算程序:
一.计划
编写一个小学生四则运算出题程序
二.开发
1.需求分析
作为一名一年级小学生的家长,我希望制作一个出题软件,完成100以内的正整数的加减法题随机产生。以便减轻我的家庭负担。
2.生成设计文档
3.设计复审
4.代码规范
注意大小写,注意缩进,括号一定要配对,注意要分行。
5.具体设计
系统开始,随机产生10道加法或者减法,计算完成后,统计做了多少道题,正确率,以及用了多少时间,系统结束。
6.具体编码
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);
}
}
————————————————
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);
}
}
————————————————
计应193王帅领