软件工程个人作业01

一、设计思想:支持整数、真分数的四则运算(需要验证结果的正确性)、题目避免重复、可定制出题的数量。

二、源程序代码

  1 import java.util.Scanner;
  2 
  3 import javax.script.ScriptEngine;
  4 import javax.script.ScriptEngineManager;
  5 import javax.script.ScriptException;
  6 
  7 
  8 public class SZYS {
  9 
 10     public static void main(String[] args) throws ScriptException {
 11         // TODO Auto-generated method stub
 12         Scanner scan=new Scanner(System.in);
 13         String fuhao[]=new String[4];
 14         fuhao[0]="+";
 15         fuhao[1]="-";
 16         fuhao[2]="*";
 17         fuhao[3]="/";
 18         
 19         int i,j,k,m=0;
 20         int M=0;int N1=0;
 21         
 22     System.out.print("请选择题目的难易程度:(请输入“较简单” or “较困难”)   ");
 23     String chengdu=scan.next();
 24     if(chengdu.equals("较简单"))
 25         {M=100;N1=10;}
 26     else if(chengdu.equals("较困难"))
 27         {M=1000;N1=100;}
 28     else{
 29         System.out.println("输入有误!");
 30     }
 31     System.out.println();
 32         System.out.print("整数的四则运算题目的数量定量:");
 33         int N=scan.nextInt();
 34         System.out.print("真分数的四则运算题目的数量定量:");
 35         int n=scan.nextInt();
 36         
 37         //Part01
 38         ScriptEngine se=new ScriptEngineManager().getEngineByName("JavaScript");
 39         int dui=0;//做对的题目
 40         String zuocuo="";//做错的题目
 41         String suan[]=new String[N];//算式
 42         double key[]=new double[N];//答案
 43         String K[]=new String[N];
 44         System.out.println();
 45         System.out.println("题目打印如下:\n");
 46         System.out.println("一、整数的四则运算。");
 47         for(i=0;i<N;i++){
 48             int x=(int)(Math.random()*M); 
 49             int x2=(int)(Math.random()*M); 
 50             int y=(int)(Math.random()*M);
 51             int y2=(int)(Math.random()*M); 
 52             int z=(int)(Math.random()*4);
 53             int zz=(int)(Math.random()*4);
 54             int zzz=(int)(Math.random()*4);
 55             String output=x+fuhao[z]+y+fuhao[zz]+x2+fuhao[zzz]+
 56             y2;
 57              suan[i]=output+" =";
 58              //key[i]=(double) se.eval(output);
 59         }
 60         
 61         //避免重复
 62         for(j=0;j<N;j++){
 63             for(k=j+1;k<N-1;k++){
 64                 if(suan[j].equals(suan[k]))
 65                 {
 66             int x2=(int)(Math.random()*M); 
 67             int x22=(int)(Math.random()*M); 
 68             int y2=(int)(Math.random()*M);
 69             int y22=(int)(Math.random()*M); 
 70             int z2=(int)(Math.random()*4);
 71             int zz2=(int)(Math.random()*4);
 72             int zzz2=(int)(Math.random()*4);
 73             
 74             String output=
 75                     suan[k]=x2+" "+fuhao[z2]+" "+y2+" "+fuhao[zz2]+" "+x22+" "+fuhao[zzz2]+" "+
 76             y22;
 77             suan[k]=output+" =";
 78             key[i]=(int)se.eval(output);
 79                 }
 80             }
 81         }
 82         for(m=0;m<N;m++){
 83             System.out.print(m+1+".  "+suan[m]);
 84             K[m]=scan.next();
 85             if(yanzheng(K[m],key[m])==1){
 86                 dui++;
 87             }
 88             else{
 89                 zuocuo+=" "+(m+1);
 90             }
 91             System.out.println();
 92         }
 93         System.out.println("做对了"+dui+"道题,题号"+zuocuo+"的答案错误。");
 94         
 95         
 96         //Part02  随机产生真分数的四则运算
 97         int dui2=0;//做对的题目
 98         String zuocuo2="";//做错的题目
 99         String suan2[]=new String[n];//算式
100         int key2[]=new int[n];//答案
101         String K2[]=new String[n];
102         System.out.println();
103         System.out.println("二、真分数的四则运算。");
104         for(i=0;i<n;i++){
105             int x0=(int) ((Math.random()*N1)+2);
106             int xx=(int) ((Math.random()*(x0-1))+1);
107             int a=gongyue(x0,xx);
108             int x00=(int)(Math.random()*N1)+2;
109             int xx0=(int)(Math.random()*(x00-1))+1;
110             int b=gongyue(x00,xx0);
111             int y0=(int)(Math.random()*N1)+2;
112             int yy=(int)(Math.random()*(y0-1)+1);
113             int c=gongyue(y0,yy);
114             int y00=(int)(Math.random()*N1)+2;
115             int yy0=(int)(Math.random()*(y00-1)+1);
116             int d=gongyue(y00,yy0);
117             int z=(int)(Math.random()*4);
118             int z0=(int)(Math.random()*4);
119             int z00=(int)(Math.random()*4);
120             
121             int p=x0/a;
122             int q=xx/a;
123             int o=x00/b;
124             int w=xx0/b;
125             int ii=y0/c;
126             int e=yy/c;
127             int u=y00/d;
128             int r=yy0/d;
129             
130             String output=q+"/"+p+" "+fuhao[z]+" "+w+"/"+o+" "+fuhao[z0]+" "+e+"/"
131             +ii+" "+fuhao[z00]+" "+r+"/"+u;
132             suan2[i]=output+" =";
133             }//for
134         
135         //避免重复
136         for(j=0;j<n;j++){
137             for(k=j+1;k<n-1;k++){
138                 if(suan2[j].equals(suan2[k]))
139                 {
140                     int x0=(int)(Math.random()*N1)+2;
141                     int xx=(int)(Math.random()*(x0-1))+1;
142                     int a=gongyue(xx,x0);
143                     int x00=(int)(Math.random()*N1)+2;
144                     int xx0=(int)(Math.random()*(x00-1))+1;
145                     int b=gongyue(xx0,x00);
146                     int y0=(int)(Math.random()*N1)+2;
147                     int yy=(int)(Math.random()*(y0-1)+1);
148                     int c=gongyue(y0,yy);
149                     int y00=(int)(Math.random()*N1)+2;
150                     int yy0=(int)(Math.random()*(y00-1)+1);
151                     int d=gongyue(y00,yy0);
152                     int z=(int)(Math.random()*4);
153                     int z0=(int)(Math.random()*4);
154                     int z00=(int)(Math.random()*4);
155                     
156                     int p=x0/a;
157                     int q=xx/a;
158                     int o=x00/b;
159                     int w=xx0/b;
160                     int ii=y0/c;
161                     int e=yy/c;
162                     int u=y00/d;
163                     int r=yy0/d;
164                     
165                     String output=q+"/"+p+" "+fuhao[z]+" "+w+"/"+o+" "+fuhao[z0]+" "+e+"/"
166                             +ii+" "+fuhao[z00]+" "+r+"/"+u;
167                     suan2[k]=output+" =";
168                     }
169             }
170         }
171         for(m=0;m<n;m++){
172             System.out.print(m+1+".  "+suan2[m]);
173             K2[m]=scan.next();
174             if(yanzheng(K2[m],key2[m])==1){
175                 dui2++;
176             }
177             else{
178                 zuocuo2+=" "+(m+1);
179             }
180             System.out.println();
181         }
182         System.out.println("做对了"+dui2+"道题,题号"+zuocuo2+"的答案错误。");
183         
184     }//main
185     //化简
186     public static int gongyue(int x,int y){
187         int gongyue=1;
188         int min=Math.min(x, y);
189         for(int i=min;i>0;i--){
190             if((x%i==0) && (y%i==0))
191             {
192                 gongyue=i;
193                break;
194             }
195         }//for
196         return gongyue;    
197     }
198     //判断输入的与答案是否相等
199     public static int yanzheng(String x,double k){
200         int m=0;
201         String n=String.valueOf(k);
202         if(x.equals(n)){
203             m=1;//1为相等
204         }
205         else
206             m=0;
207         return m;
208     }
209 }//end

 

三、运行结果截图

四、分析上课未能完成原因

要说一说现在和以前的差别,最主要的是态度和状态,以前总是感觉编程和自己没有什么多大的关系,上大二了就感觉毕业找工作和自己不远了,我的天,一切就这么活生生的展现在自己的眼前,有了恐惧感,没办法,只能自己定下决心,要努力啦,你没有别的路了。

出现的问题:理解经别人点拨出现问题,只能进行四个数的运算,运算数字的个数应该是随机的。

posted on 2017-03-02 19:29  鲨鱼姑娘  阅读(149)  评论(0编辑  收藏  举报