java-四则运算二

1.实验目的:是否有乘除法,括号,多项式运算。

2.思路:利用简单的循环switch语句进行循环输出随机数

3.程序源代码:

package jiajianchengchu;
import java.util.*;
public class jisuan {
    //定义一个用来约分的函数
public static String yuefen(int a,int b){
        int y = 1;
        for(int i=a;i>=1;i--){
            if(a%i==0&&b%i==0){
                y = i;
                break;
            }
        }
        int z = a/y;
        int m = b/y;
        if(z==0) {
            return "0";
        }
        return ""+z+"/"+m;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int x,y,i,a1,C;//a代表计算类型,x代表题目数量,y代表数值的范围,i为循环变量,d代表是否有乘除法
        int d1=0;
        String d = null;//d用来储存答案
        System.out.println("1、整数计算式,2、真分数计算式");
        Scanner scan1=new Scanner(System.in);
        a1=scan1.nextInt();
if(a1==1){
        System.out.println("请输入题目的数量");
        Scanner scan2=new Scanner(System.in);
        x=scan2.nextInt();
        int daan[]=new int [x];
        System.out.println("请输入数值的范围");
        y=scan2.nextInt();
        System.out.println("是否有乘除法?(是输入1,否则输入0)");
        C=scan2.nextInt();
        if(C==1){
            for(i=0;i<x;i++){
                int a=(int)(Math.random()*y);//随机生成一个1-10的整数
                int b=(int)(Math.random()*y);//随机生成一个1-10的整数
                int c=(int)(Math.random()*3);//随机生成一个1-4的整数,0表示加法,1表示减法,2表示乘法,3表示除法
                if(c==0)
                {
                    d1=a+b;
                    System.out.print(a+"+"+b+"=       ");
                }
                if(c==1)
                {
                    d1=a-b;
                    System.out.print(a+"-"+b+"=       ");
                }
                if(c==2)
                {
                    d1=a*b;
                    System.out.print(a+"*"+b+"=       ");
                }
                if(c==3)
                {
                    d1=a/b;
                    System.out.print(a+"/"+b+"=       ");
                }
                if((i+1)%3==0){
                    System.out.println();
                }
                daan[i]=d1;
            }
            System.out.println("是否显示答案(显示请输入1)");
            if(scan2.nextInt()==1){
                for(i=0;i<x;i++){
                System.out.print(daan[i]+"     ");
                }
            }
            }
        if(C==0){
            for(i=0;i<x;i++){
                int a=(int)(Math.random()*y);//随机生成一个1-的整数
                int b=(int)(Math.random()*y);//随机生成一个1-10的整数
                int c=(int)(Math.random()*2);//随机生成一个1-4的整数,0表示加法,1表示减法
                if(c==0)
                {
                    d1=a+b;
                    System.out.print(a+"+"+b+"=       ");
                }
                if(c==1)
                {
                    d1=a-b;
                    System.out.print(a+"-"+b+"=       ");
                }
                if((i+1)%3==0){
                    System.out.println();
                }
                daan[i]=d1;
            }
            System.out.println("     ");
            System.out.println("是否显示答案(显示请输入1)");
            if(scan2.nextInt()==1){
                for(i=0;i<x;i++){
                System.out.print(daan[i]+"     ");
                }
            }
        }
        }   
if(a1==2){
            int M,Z;
            System.out.println("请输入题目的数量");
            Scanner scan2=new Scanner(System.in);
            x=scan2.nextInt();
            String daan[]=new String [x];
            int x1,x2,B,m1,m2;
            System.out.println("请输入分母数值的范围");
            B=scan2.nextInt();
            System.out.println("是否有乘除法?(是输入1,否则输入0)");
            C=scan2.nextInt();
            if(C==1){
                for(i=0;i<x;i++){
                    m1=1+(int)(Math.random()*B);//随机生成一个小于B的分母
                    x1=1+(int)(Math.random()*m1);//生成一个比分母小的分子,实现真分数
                    m2=1+(int)(Math.random()*B);//随机生成一个小于B的分母
                    x2=1+(int)(Math.random()*m2);//生成一个比分母小的分子,实现真分数
                    int c=(int)(Math.random()*3);//生成运算符
                    if(c==0){
                        Z=x1*m2+x2*m1;
                        M=m1*m2;
                        d=yuefen(Z,M);
                        System.out.print(x1+"/"+m1+"+"+x2+"/"+m2+"=       ");
                    }
                    if(c==1){
                        Z=x1*m2-x2*m1;
                        M=m1*m2;
                        d=yuefen(Z,M);
                        System.out.print(x1+"/"+m1+"-"+x2+"/"+m2+"=       ");
                    }
                    if(c==2){
                        Z=x1*x2;
                        M=m1*m2;
                        d=d=yuefen(Z,M);
                        System.out.print(x1+"/"+m1+"*"+x2+"/"+m2+"=       ");
                    }
                    if(c==3){
                        Z=m1*x2;
                        M=m2*x1;
                        d=d=yuefen(Z,M);
                        System.out.print(x1+"/"+m1+"/"+x2+"/"+m2+"=       ");
                    }
                    if((i+1)%3==0){
                        System.out.println();
                    }
                    daan[i]=d;
                     
                }
                System.out.println("是否显示答案(显示请输入1)");
                if(scan2.nextInt()==1){
                    for(i=0;i<x;i++){
                    System.out.print(daan[i]+"     ");
                    }
                }
            }
            if(C==0){
                for(i=0;i<x;i++){
                    m1=1+(int)(Math.random()*B);//随机生成一个小于B的分母
                    x1=1+(int)(Math.random()*m1);//生成一个比分母小的分子,实现真分数
                    m2=1+(int)(Math.random()*B);//随机生成一个小于B的分母
                    x2=1+(int)(Math.random()*m2);//生成一个比分母小的分子,实现真分数
                    int c=(int)(Math.random()*2);//生成运算符
                    if(c==0){
                        Z=x1*m2+x2*m1;
                        M=m1*m2;
                        d=yuefen(Z,M);
                        System.out.print(x1+"/"+m1+"+"+x2+"/"+m2+"=       ");
                    }
                    if(c==1){
                        Z=x1*m2-x2*m1;
                        M=m1*m2;
                        d=yuefen(Z,M);
                        System.out.print(x1+"/"+m1+"-"+x2+"/"+m2+"=       ");
                    }
                    if((i+1)%3==0){
                        System.out.println();
                    }
                    daan[i]=d;
                     
                }
                 System.out.println("    ");
                System.out.println("是否显示答案(显示请输入1)");
                if(scan2.nextInt()==1){
                    for(i=0;i<x;i++){
                    System.out.print(daan[i]+"     ");
                    }
                }
            }
        }
    }
}

4.实验结果截图:

java-四则运算二java-四则运算二java-四则运算二

5.实验总结:

对于真分数的计算方式不熟悉,需要请教其他同学;

对于存在多项式的运算不熟悉。

上一篇:MATLAB符号运算 分类: 图像处理 2015-07-31 22:53 3人阅读 评论(0) 收藏


下一篇:关于Javascript中声明变量、函数的笔记