四则运算功能细化

设计思想:

当编程时感觉做法完全不像当初设计思路里设计的一样,因为设计思路里没有面对这个程序,没有面对这么具体的问题.

不重复的实现,建三个数组,并每次生成随机数后判断a[i]、b[i]、c[i]与上边已有的a[i]、b[i]、c[i]是否相同,若相同重新生成a[i]、b[i]、c[i]。

for(j=0;j<z;j++)

            {

                if(q==a[j]&&w==b[j]&&e==c[j])       ////判断是否相同

                    {a[i]=random.nextInt(100);      ////重新产生a[i]

                    b[i]=random.nextInt(100);       ////重新产生b[i]

                    c[i]=random.nextInt()%4+1;} ////重新产生c[i]

            }

可定制的实现,输入一个n,比如n=4,当(i+1)%4==0时,输出一个换行,这样就实现了每行有4个算式。

是否有乘除法的实现,若有乘除法,c取1234,若无乘除c取12.

c取1234时:

if      (c[i]==1) s='+';

        else if (c[i]==2) s='-';

        else if (c[i]==3) s='*';

        else s='/';

c取12时:

if      (c[i]==1) s='+';

        else s='-';

 

实现代码:

package 四则运算;

 

import java.util.Random;

import java.util.Scanner;

 

public class aa {

 

         public static void main(String[] args) {

                   // TODO Auto-generated method stub

                   int i,n,j,m,aa,r = 0;

                   Scanner scanner = new Scanner(System.in);

                   System.out.print("要输出多少道四则运算题,然后敲回车");

                   aa= scanner.nextInt();

                   i=aa;

                   System.out.print("请输入n(每行显示n个式子),然后敲回车");

                   n= scanner.nextInt();

                   System.out.print("是否有乘除法(有 1    无 0),然后敲回车");

                   m= scanner.nextInt();

                   if(m==1)

                            {

                            System.out.print("除法有无余数(有 1    无 0),然后敲回车");

                            r= scanner.nextInt();

                            }

                   scanner.close();

                   int a[],b[],c[];

                   a=new int[i];b=new int[i];c=new int[i];

                   for(i=0;i<aa;i++)

                   {

                   int z,q,w,e;z=i;

                   char s;

                   Random random = new Random();

                   a[i]=random.nextInt(100);

                   b[i]=random.nextInt(100);

 

                   if(m==1)

                   {

                            if(r==1)//有余数

                            {

                                     c[i]=random.nextInt()%4+1;

                                     q=a[i];w=b[i];e=c[i];

                                     for(j=0;j<z;j++)

                                     {

                                               if(q==a[j]&&w==b[j]&&e==c[j])

                                                        {a[i]=random.nextInt(100);

                                                        b[i]=random.nextInt(100);

                                                        c[i]=random.nextInt()%4+1;}

                                     }

                                     if               (c[i]==1) s='+';

                                     else if (c[i]==2) s='-';

                                     else if (c[i]==3) s='*';

                                     else s='/';

                            }

                            else //无余数

                            {

                            a[i]=random.nextInt(100);

                            b[i]=random.nextInt(100);

                            c[i]=random.nextInt()%4+1;

                            q=a[i];w=b[i];e=c[i];

                            for(j=0;j<z;j++)

                                     {

                                               if(q==a[j]&&w==b[j]&&e==c[j])

                                                        {a[i]=random.nextInt(100);

                                                        b[i]=random.nextInt(100);

                                                        c[i]=random.nextInt()%4+1;}

                                     }

                            if               (c[i]==1) s='+';

                            else if (c[i]==2) s='-';

                            else if (c[i]==3) s='*';

                            else {s='/';a[i]=a[i]/b[i]*b[i];}

                            }

                   }

                   else

                   {

                   c[i]=random.nextInt()%2+1;

                   q=a[i];w=b[i];e=c[i];

                   for(j=0;j<z;j++)

                            {

                                     if(q==a[j]&&w==b[j]&&e==c[j])

                                               {a[i]=random.nextInt(100);

                                               b[i]=random.nextInt(100);

                                               c[i]=random.nextInt()%2+1;}

                            }

                   if               (c[i]==1) s='+';

                   else s='-';

                   }

                  

                   System.out.print(a[i]);

                   System.out.print(s);

                   System.out.print(b[i]);

                   System.out.print("=     ");

                   if((i+1)%n==0)

                   System.out.println("");

                   }

         }

 

}

 

结果截图:

 

 

 

心得总结:java技术需要提高,简单的东西也会出错,而且吧,鼓捣半天都改不对,很是气愤

 

时间记录日志

学生:

周盼超

 

日期:

3/19/2015

 

教师:

王建民

 

课程:

PSP

 

         

 

日期

开始时间

结束时间

净时

活动

备注

3月13日

10点10分

12点

100分钟

讲师授课

 

3月15日

9点

9点20分

20分钟

编写程序

 

3月17日

12点

13点30分

90分钟

编写程序

 

3月18日

21点30分

22点

30分钟

制作时间记录日志模板

 

posted @ 2015-03-19 16:38  非非是  阅读(233)  评论(0编辑  收藏  举报