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.实验结果截图:

5.实验总结:

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

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

转载于:https://www.cnblogs.com/ziyixuedie/p/6537911.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值