软件工程个人作业01
题目:像二柱子那样,花二十分钟写一个能自动生成三十道小学四则运算题目的 “软件”,要求:除了整数以外,还要支持真分数的四则运算(需要验证结果的正确性)、题目避免重复、可定制出题的数量。(有能力者改编成网页版)
设计思路:首先要求用户输入要得到的题目的数目然后确定这些题目的上下限,然后再从这个范围内取出随机数确定运算的两个数 ,再从四个运算符中随机取出一个如果是除则判断分母是否是0,然后输出结果。
代码:
package 四则运算;
//马建宁20153122写于2017.3.1
import java.util.Scanner;
public class Caculate {
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean tag = true;
Scanner scan = new Scanner(System.in);
int size=0 ,big=0,end=0;//= scan.nextInt();
while(tag){
try{
System.out.print("输入要得到的题目数:");
size = scan.nextInt();
System.out.println("下限是:");
big = scan.nextInt();
System.out.println("上限是:");
end = scan.nextInt();
tag = false;
}catch(Exception e){
System.out.println("输入内容有误重新输入!");
tag = true;
}
}
String array[] = new String[size];
for(int i = 0; i < size; i++){
array[i] = function(big,end);
int j = i-1;
while(j >= 0){
if(array[j].equals(array[i])){
array[i] = function(big,end);
j = i;
}
j--;
}
System.out.println((i+1)+": "+array[i]);
}
scan.close();
}
public static String function(int big,int size){
String[] operator = new String[5];
operator[0] = "+";
operator[1] = "-";
operator[2] = "*";
operator[3] = "/";
operator[4] = "+";
int index = (int)(Math.random() * 4);
int num1 = (int)(Math.random() * (size-big))+big;
int num2 = (int)(Math.random() * (size-big))+big;
while(num2==0 && index==3)
num2 = (int)(Math.random() * (size-big))+big;
return num1 + " " + operator[index] + " " + num2 + " =";
}
}