• 软件工程个人作业03


    设计思想:

    在2的基础上加上一个判断,在题目生成输出后予以判断,如果是有余数或者是分数就分别判断商及余数或者分子分母是否正确。

    程序源码:

    import java.util.Random;
    
    import javax.swing.*;
    
    public class Arithmetic 
    {
        static void add(int limit1,int limit2,int boolminus/*int boolremainder,int boolmultiply*/)
        {
            int number1=new Random().nextInt(limit2-limit1+1)+limit1;
            int number2=new Random().nextInt(limit2-limit1+1)+limit1;
            if(boolminus==0)
            {
                if(limit1<0||limit2<0)
                {
                    System.out.println("您输入的范围有误");
                }
                else 
                {
                    System.out.println(number1+"+"+number2+"=");
                    String answer=JOptionPane.showInputDialog("please input answer");
                    int answer1=Integer.parseInt(answer);
                    if(answer1==number1+number2)
                        System.out.println("right!");
                    else
                        System.out.println("wrong!");
                }
            }
            else
            {
                System.out.println(number1+"+"+number2+"=");
                String answer=JOptionPane.showInputDialog("please input answer");
                int answer1=Integer.parseInt(answer);
                if(answer1==number1+number2)
                    System.out.println("right!");
                else
                    System.out.println("wrong!");
            }
        }
        
        static void subtract(int limit1,int limit2,int boolminus)
        {
            int number1=new Random().nextInt(limit2-limit1+1)+limit1;
            int number2=new Random().nextInt(limit2-limit1+1)+limit1;
            if(boolminus==0)
            {
                while(true)
                {
                    if(number1<number2)
                    {
                        number1=new Random().nextInt(limit2-limit1+1)+limit1;
                        number2=new Random().nextInt(limit2-limit1+1)+limit1;
                    }
                    else 
                    {
                        break;
                    }
                }
                if(limit1<0||limit2<0)
                {
                    System.out.println("您输入的范围有误");
                }
                
                else if(limit1>=0||limit2>=0||number1>=number2)
                {
                    System.out.println(number1+"-"+number2+"=");
                    String answer=JOptionPane.showInputDialog("please input answer");
                    int answer1=Integer.parseInt(answer);
                    if(answer1==number1-number2)
                        System.out.println("right!");
                    else
                        System.out.println("wrong!");
                }
            }
            else 
            {
                System.out.println(number1+"-"+number2+"=");
                String answer=JOptionPane.showInputDialog("please input answer");
                int answer1=Integer.parseInt(answer);
                if(answer1==number1-number2)
                    System.out.println("right!");
                else
                    System.out.println("wrong!");
            }
        }
        
        static void multiply(int limit1,int limit2/*int boolremainder,*/)
        {
            int number1=new Random().nextInt(limit2-limit1+1)+limit1;
            int number2=new Random().nextInt(limit2-limit1+1)+limit1;
            System.out.println(number1+"*"+number2+"=");
            String answer=JOptionPane.showInputDialog("please input answer");
            int answer1=Integer.parseInt(answer);
            if(answer1==number1*number2)
                System.out.println("right!");
            else
                System.out.println("wrong!");
        }
        
        static void divide(int limit1,int limit2,int boolremainder)
        {
            int number1=new Random().nextInt(limit2-limit1+1)+limit1;
            int number2=new Random().nextInt(limit2-limit1+1)+limit1;
            if(boolremainder==0)
            {
                while(true)
                {
                    if(number1%number2!=0)
                    {
                        number1=new Random().nextInt(limit2-limit1+1)+limit1;
                        number2=new Random().nextInt(limit2-limit1+1)+limit1;
                    }
                    else
                    {
                        System.out.println(number1+"/"+number2+"=");
                        String answer=JOptionPane.showInputDialog("please input answer");
                        int answer1=Integer.parseInt(answer);
                        if(answer1==number1+number2)
                            System.out.println("right!");
                        else
                            System.out.println("wrong!");
                    }
                }
            }
            else
            {
                System.out.println(number1+"/"+number2+"=");
                String answer1=JOptionPane.showInputDialog("请输入商");
                int answer11=Integer.parseInt(answer1);
                String answer2=JOptionPane.showInputDialog("请输入余数");
                int answer22=Integer.parseInt(answer2);
                if(answer11==number1/number2&&answer22==number1%number2)
                    System.out.println("right!");
                else
                    System.out.println("wrong!");
            }
        }
        static void fraction(int limit1,int limit2,int numbers,int boolmultiply)
        {
            int i,j,k;
            if(boolmultiply==0)
            { 
                for(int o=numbers;o>0;o--)
                {
                     int number1=new Random().nextInt(limit2-limit1+1)+limit1;
                     int number2=0;
                     while(true)
                     {
                         number2=new Random().nextInt(limit2-limit1+1)+limit1;
                         if(number2!=0)
                             break;
                     }
                     if(number1<number2)
                         i=number1;
                     else i=number2;
                     for(int ii=i+1;ii>=1;ii--)
                     {
                        if(number1%ii==0&&number2%ii==0)
                        {
                            number1=number1/ii;
                            number2=number2/ii;
                            break;
                        }
                     }
                     
                     int number3=new Random().nextInt(limit2-limit1+1)+limit1;
                     int number4=0;
                     while(true)
                     {
                         number4=new Random().nextInt(limit2-limit1+1)+limit1;
                         if(number4!=0)
                             break;
                     }
                     if(number3<number4)
                         j=number3;
                     else j=number4;
                     for(int jj=j+1;jj>=1;jj--)
                     {
                        if(number3%jj==0&&number4%jj==0)
                        {
                            number3=number3/jj;
                            number4=number4/jj;
                            break;
                        }
                     }
                    k=new Random().nextInt(2);
                    if(k==0)
                    {
                        System.out.println(number1+"/"+number2+"+"+number3+"/"+number4+"=");
                        int fenzi=number1*number4+number2*number3;
                        int fenmu=number2*number4;
                        int small1;
                        if(fenzi<fenmu)
                            small1=fenzi;
                        else small1=fenmu;
                        for(int small11=small1+1;small11>=1;small11--)
                            {
                            if(fenzi%small11==0&&fenmu%small11==0)
                               {
                                   fenzi=fenzi/small11;
                                   number2=number2/small11;
                                   break;
                               }
                            }
                        String answer=JOptionPane.showInputDialog("请输入分子");
                        int answer1=Integer.parseInt(answer);
                        String answer0=JOptionPane.showInputDialog("请输入分母");
                        int answer10=Integer.parseInt(answer0);
                        if(answer1==fenzi&&answer10==fenmu)
                            System.out.println("right!");
                        else
                            System.out.println("wrong!");
                    }
                    else if(k==1)
                    {
                        System.out.println(number1+"/"+number2+"-"+number3+"/"+number4+"=");
                        int fenzi=number1*number4-number2*number3;
                        int fenmu=number2*number4;
                        int small1;
                        if(fenzi<fenmu)
                            small1=fenzi;
                        else small1=fenmu;
                        for(int small11=small1+1;small11>=1;small11--)
                            {
                            if(fenzi%small11==0&&fenmu%small11==0)
                               {
                                   fenzi=fenzi/small11;
                                   number2=number2/small11;
                                   break;
                               }
                            }
                        String answer=JOptionPane.showInputDialog("请输入分子");
                        int answer1=Integer.parseInt(answer);
                        String answer0=JOptionPane.showInputDialog("请输入分母");
                        int answer10=Integer.parseInt(answer0);
                        if(answer1==fenzi&&answer10==fenmu)
                            System.out.println("right!");
                        else
                            System.out.println("wrong!");
                    }
                }
            }
            else
            {
                for(int o=numbers;o>0;o--)
                {
                    int number1=new Random().nextInt(limit2-limit1+1)+limit1;
                    int number2=0;
                        while(true)
                        {
                            number2=new Random().nextInt(limit2-limit1+1)+limit1;
                            if(number2!=0)
                                break;
                        }
                    if(number1<number2)
                        i=number1;
                    else i=number2;
                    
                    int number3=new Random().nextInt(limit2-limit1+1)+limit1;
                    int number4=0;
                        while(true)
                        {
                            number4=new Random().nextInt(limit2-limit1+1)+limit1;
                            if(number4!=0)
                                break;
                        }
                    if(number3<number4)
                        j=number3;
                    else j=number4;
                    for(int ii=i+1;ii>=1;ii--)
                        {
                        if(number1%ii==0&&number2%ii==0)
                        {
                            number1=number1/ii;
                            number2=number2/ii;
                            break;
                        }
                        }
                    for(int jj=j+1;jj>=1;jj--)
                        {
                        if(number3%jj==0&&number4%jj==0)
                        {
                            number3=number3/jj;
                            number4=number4/jj;
                            break;
                        }
                        }
                    k=new Random().nextInt(4);
                    if(k==0)
                    {
                        System.out.println(number1+"/"+number2+"+"+number3+"/"+number4+"=");
                        int fenzi=number1*number4+number2*number3;
                        int fenmu=number2*number4;
                        int small1;
                        if(fenzi<fenmu)
                            small1=fenzi;
                        else small1=fenmu;
                        for(int small11=small1+1;small11>=1;small11--)
                            {
                            if(fenzi%small11==0&&fenmu%small11==0)
                               {
                                   fenzi=fenzi/small11;
                                   number2=number2/small11;
                                   break;
                               }
                            }
                        String answer=JOptionPane.showInputDialog("请输入分子");
                        int answer1=Integer.parseInt(answer);
                        String answer0=JOptionPane.showInputDialog("请输入分母");
                        int answer10=Integer.parseInt(answer0);
                        if(answer1==fenzi&&answer10==fenmu)
                            System.out.println("right!");
                        else
                            System.out.println("wrong!");
                    }
                    else if(k==1)
                    {
                        System.out.println(number1+"/"+number2+"-"+number3+"/"+number4+"=");
                        int fenzi=number1*number4-number2*number3;
                        int fenmu=number2*number4;
                        int small1;
                        if(fenzi<fenmu)
                            small1=fenzi;
                        else small1=fenmu;
                        for(int small11=small1+1;small11>=1;small11--)
                            {
                            if(fenzi%small11==0&&fenmu%small11==0)
                               {
                                   fenzi=fenzi/small11;
                                   number2=number2/small11;
                                   break;
                               }
                            }
                        String answer=JOptionPane.showInputDialog("请输入分子");
                        int answer1=Integer.parseInt(answer);
                        String answer0=JOptionPane.showInputDialog("请输入分母");
                        int answer10=Integer.parseInt(answer0);
                        if(answer1==fenzi&&answer10==fenmu)
                            System.out.println("right!");
                        else
                            System.out.println("wrong!");
                    }
                    else if(k==2)
                    {
                        System.out.println(number1+"/"+number2+"*"+number3+"/"+number4+"=");
                        int fenzi=number1*number3;
                        int fenmu=number2*number4;
                        int small1;
                        if(fenzi<fenmu)
                            small1=fenzi;
                        else small1=fenmu;
                        for(int small11=small1+1;small11>=1;small11--)
                            {
                            if(fenzi%small11==0&&fenmu%small11==0)
                               {
                                   fenzi=fenzi/small11;
                                   number2=number2/small11;
                                   break;
                               }
                            }
                        String answer=JOptionPane.showInputDialog("请输入分子");
                        int answer1=Integer.parseInt(answer);
                        String answer0=JOptionPane.showInputDialog("请输入分母");
                        int answer10=Integer.parseInt(answer0);
                        if(answer1==fenzi&&answer10==fenmu)
                            System.out.println("right!");
                        else
                            System.out.println("wrong!");
                    }
                    else 
                    {
                        System.out.println(number1+"/"+number2+"/"+number3+"/"+number4+"=");
                        int fenzi=number1*number4;
                        int fenmu=number2*number3;
                        int small1;
                        if(fenzi<fenmu)
                            small1=fenzi;
                        else small1=fenmu;
                        for(int small11=small1+1;small11>=1;small11--)
                            {
                            if(fenzi%small11==0&&fenmu%small11==0)
                               {
                                   fenzi=fenzi/small11;
                                   number2=number2/small11;
                                   break;
                               }
                            }
                        String answer=JOptionPane.showInputDialog("请输入分子");
                        int answer1=Integer.parseInt(answer);
                        String answer0=JOptionPane.showInputDialog("请输入分母");
                        int answer10=Integer.parseInt(answer0);
                        if(answer1==fenzi&&answer10==fenmu)
                            System.out.println("right!");
                        else
                            System.out.println("wrong!");
                    }
                }
            }
        }
        
        public static void main(String[] args)
        {
            String choice0=JOptionPane.showInputDialog("是否有分数运算(有输入1,无输入0)");
            String choice1=JOptionPane.showInputDialog("是否有乘除法(有输入1,无输入0)");
            String choice2=JOptionPane.showInputDialog("请输入数值范围下限");
            String choice21=JOptionPane.showInputDialog("请输入数值范围上限");
            String choice5=JOptionPane.showInputDialog("需要多少道题");
            int choice00=Integer.parseInt(choice0);
            int choice11=Integer.parseInt(choice1);
            int limit1=Integer.parseInt(choice2);
            int limit2=Integer.parseInt(choice21);
            int i=Integer.parseInt(choice5);
            if(choice00==0)
            {
                String choice3=JOptionPane.showInputDialog("加法有无负数(有输入1,无输入0)");
    
                int choice33=Integer.parseInt(choice3);
                
                
                if(choice11==0)
                {
                    for(;i>0;i--)
                    {
                        int j=new Random().nextInt(2);
                        if(j==0)
                            add(limit1,limit2,choice33);
                        else 
                            subtract(limit1,limit2,choice33);
                    }
                }
                else if(choice11==1)
                {
                    String choice4=JOptionPane.showInputDialog("除法有无余数(有输入1,无输入0)");
                    int choice44=Integer.parseInt(choice4);
                    for(;i>0;i--)
                    {
                        int j=new Random().nextInt(4);
                        if(j==0)
                            add(limit1,limit2,choice33);
                        else if(j==1)
                            subtract(limit1,limit2,choice33);
                        else if(j==2)
                            multiply(limit1,limit2);
                        else if(j==3)
                            divide(limit1,limit2,choice44);
                    }
                }
            }
            else
            {
                fraction(limit1,limit2,i,choice11);
            }
        }
    }

    记录日志

    日期 课堂 编程 读书 博客 日总结
    3.21 120   100   220
    3.22     100   100
    3.26   60   30 90
    周总结 120 60 200 30 410

    缺陷:

    由于2的完成度较低,3的缺陷大部分基于2,其他未有明显缺陷。另代码重复较多,已有优化思路。

    很抱歉由于本周事务冗杂,未有足够时间放在程序上。

  • 相关阅读:
    MVC 路由规则
    MVC系统过滤器、自定义过滤器
    MVC部署
    MVC 读书笔记
    C# 调用 Web Service
    RESTful 架构
    WebSocket C# Demo
    C# 编写服务 Windows service
    C# 调用FFmpeg 根据图片合成视频
    Socket 编程示例(二)
  • 原文地址:https://www.cnblogs.com/a1264393659/p/5322592.html
Copyright © 2020-2023  润新知