软件工程个人作业01

程序设计思想

         应用结构化的程序设计思想,首先将如何生成一个计算式封装称为一个方法。当然为了控制计算范围可以设置两个形式参数表示运算数的最大值和最小值。因为需要的到一个算式,所以该方法的返回值应该是字符串类型,这样也可以方便判断是否出现相同的题目。在主方法中循环调用该方法得到指定个数的算式,所有算式存放在一个字符串数组中,每生成一个算式需要和以前生成的算式都进行比较,如果出现相同的则需要重新生成。最后控制输出格式打印输出即可。

程序源代码

import java.util.Scanner;

public class Main {

       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 + " =";

       }

}

程序运行结果截图

截图1:

 

截图2:

 

截图3:

 

分析上课为什么没有做完

         虽然我上课作的比较简陋,输入异常情况也没有处理,但是我做完了,功能基本实现了。

原文地址:https://www.cnblogs.com/maosonglin/p/6483796.html