软件工程作业03

程序设计思想:

根据这次程序的设计,把程序输出分为两个文件,文件里边输出为算式和算式结果,另外还得存在判断对错;

输出算式对错题号和题数,括号的随机产生,在算式产生的时候有符号的限制。

源代码:

  1 package test;
  2 import java.util.Scanner;
  3 
  4 public class ssss {
  5 
  6     public static void main(String[] args) {
  7         // TODO Auto-generated method stub
  8         Scanner num=new Scanner(System.in);
  9         System.out.println("请选择题目类型:");
 10         System.out.println("1.整数    2.真分数");
 11         int type=num.nextInt();
 12         if(type==1)zheng();
 13         if(type==2)fen();
 14         
 15     }
 16     //整数四则运算
 17     public static void  zheng()
 18     {   
 19         Scanner num=new Scanner(System.in);
 20         System.out.println("请输入数值范围起始值:");
 21         int fan1=num.nextInt();
 22         System.out.println("请输入数值范围终点值:");
 23         int fan2=num.nextInt();
 24         
 25         System.out.println("请输入出题数量:");
 26         int n = num.nextInt();
 27         
 28         String timu[]=new String [n];
 29         String q1="";
 30             for(int i=0;i<n;i++)
 31             {   
 32                 boolean flag=true;
 33                 String b1="";
 34             int x=(int)(Math.random()*fan2+fan1);//第一个数
 35             int y=(int)(Math.random()*fan2+fan1);//第二个数
 36             int a=(int)(Math.random() * 4);//用来判断四则运算的符号
 37             
 38             if(a==0){q1=x+"+"+y+"=";        
 39             b1+=(x+y);}
 40         
 41             if(a==1){
 42                 while(x<y){x=(int)(Math.random()*fan2+fan1); y=(int)(Math.random()*fan2+fan1);}
 43                 q1=x+"-"+y+"=";
 44             b1+=(x-y);}
 45             
 46             if(a==2){q1=x+"*"+y+"=";
 47             b1+=(x*y);}
 48             
 49             if(a==3){
 50                 while(y==0){y=(int)(Math.random()*fan2+fan1);}
 51                 q1=x+"/"+y+"=";
 52             b1=yuefen(x,y);
 53             }
 54              int j;
 55                 for(j=0;j<i;j++)
 56                     {
 57                        if(q1.equals(timu[j]))
 58                        {
 59                            i--;
 60                            flag=false;
 61                            break;
 62                        }
 63                     }
 64                 if(flag){
 65                     timu[i]=q1;
 66                 
 67                     System.out.print(q1);
 68                 
 69             String c1=num.next();
 70             if(c1.equals(b1))
 71             {
 72                 System.out.println(" 答案正确");
 73             }
 74             else
 75             {
 76                 System.out.println(" 答案错误,正确答案为:"+b1);
 77             }
 78             }}
 79             }
 80     //真分数四则运算
 81     public static void fen()
 82     {   
 83         Scanner num=new Scanner(System.in);
 84         System.out.println("请输入分母数值范围起始值:");
 85         int fan1=num.nextInt();
 86         System.out.println("请输入分母数值范围终点值:");
 87         int fan2=num.nextInt();
 88         
 89         System.out.println("请输入出题数量:");
 90         int n = num.nextInt();
 91         
 92         String timu[]=new String [n];
 93 
 94         for(int i=0;i<n;i++)
 95         {   
 96             String k2="";
 97             boolean flag=true;
 98             int z1,z2;
 99             int b1;
100             int c1;
101             int x1=(int)(Math.random()*fan2);//分子
102             int y1=(int)(Math.random()*fan2+fan1+1);//分母
103             
104             
105             int x2=(int)(Math.random()*fan2);//分子
106             int y2=(int)(Math.random()*fan2+fan1+1);//分母
107             
108             
109             String q1="";
110             int a=(int)(Math.random() * 4);//用来判断四则运算的符号
111             if(x2==0&&a==3) i--;
112             else{
113         if(a==0) {q1=x1+"/"+y1+"+"+x2+"/"+y2+"=";
114         b1=x1*y2+x2*y1;c1=y1*y2;
115         k2=yuefen(b1,c1);}
116       
117         if(a==1) {while((x1*y2-x2*y1)<0){x1=(int)(Math.random()*fan2);y1=(int)(Math.random()*fan2+fan1+1);
118         x2=(int)(Math.random()*fan2);
119         y2=(int)(Math.random()*fan2+fan1+1);}
120             q1=x1+"/"+y1+"-"+x2+"/"+y2+"=";
121         b1=x1*y2-x2*y1;c1=y1*y2;
122         k2=yuefen(b1,c1);}
123         
124         if(a==2) {q1="("+x1+"/"+y1+")"+"*"+"("+x2+"/"+y2+")"+"=";
125         b1=x1*x2;c1=y1*y2;
126         k2=yuefen(b1,c1);}
127         
128         if(a==3) {
129             while(y1*x2==0){
130                 x1=(int)(Math.random()*fan2);
131                 x2=(int)(Math.random()*fan2);
132                 y1=(int)(Math.random()*fan2+fan1+1);
133                 y2=(int)(Math.random()*fan2+fan1+1);
134             }
135             q1="("+x1+"/"+y1+")"+"/"+"("+x2+"/"+y2+")"+"=";   
136         b1=x1*y2;c1=x2*y1;
137         k2=yuefen(b1,c1);}
138         
139         int j;
140         for(j=0;j<i;j++)
141             {
142                if(q1.equals(timu[j]))
143                {
144                    i--;
145                    flag=false;
146                    break;
147                }
148             }
149         if(flag){
150             timu[i]=q1;
151         
152             System.out.print(q1);
153             String k1=num.next();
154             if(k1.equals(k2))
155             {
156                 System.out.println(" 答案正确");
157             }
158             else
159             {
160                 System.out.println(" 答案错误,正确答案为:"+k2);
161             }}
162         }            
163         }
164  }
165 
166     //对结果进行约分的函数
167     public static String yuefen(int a,int b)//a为分子,b为分母
168     {     
169           int  n = 0,z1,z2;
170           String z3="";
171           if(a==0) z3="0";
172           else{
173           int s=a<b?a:b;
174           for(int i=s;i>0;i--)
175           {
176               if(a%i==0&&b%i==0){
177               n=i;
178               break;}
179           }
180           z1=a/n;z2=b/n;
181           if(z1<z2){
182           z3+=z1;
183           z3+="/";
184           z3+=z2;}
185           else if(z1%z2==0){z3+=(z1/z2);}
186           else if(z1>z2){
187               int k;
188               k=z1/z2;
189               z3+=k;
190               z3+="'";
191               z3+=(z1-z2*k);
192               z3+="/";
193               z3+=z2;
194           }
195           }
196           return z3;
197     }
198     }

输出结果:

整数运算:

分数运算:

个人项目体会:

这次编写过程中出现一些大的错误,还是没有克服:例如数据库不会运用,无法创建文件。现在还是不行,发现自身存在很多很多的问题。需要大量填补空白,不能再这样下去了。

原文地址:https://www.cnblogs.com/bai123/p/6566820.html