second pairing assignment

##Four arithmetic generators - driver

1. The subject and requirements:

It can automatically generate four arithmetic practice questions. The number of questions can be
customized . The
user can choose the operator. The
user can set the maximum number (such as within 10, within 100, etc. ) ) It is best to provide a graphical user interface (choose according to your own ability, mainly to complete the above functions) ------



2. Part of the function code and function:

1. Set static variables:

static int TMNumber;              //题目数量
    static int f;                           //判断符号(0表示只有+、-,1表示只有*、/,2表示运算混合)
    static int MaxNumber;           //计算的最大值
    static int k;                           //判断是否有(  )
    static int x;                               //判断是否有小数
    static int p;                           //判断输出方式

2. User-defined functions:

    public static void USERSet() {
        System.out.println("请决定出题数:");
        Scanner in=new Scanner(System.in);
        TMNumber=in.nextInt();
        System.out.println("请输入0或1决定数学题所含有的符号(0表示只有+、-,1表示还有*、/):");
        f=in.nextInt();
        System.out.println("请决定运算的最大值不超过多少:");
        MaxNumber=in.nextInt();
        System.out.println("请输入0或1来决定算式中是否含有()1代表有,0代表无:");
        k=in.nextInt();
        System.out.println("请输入0或1来决定算式中是否含有小数,1代表有,0代表无:");
        x=in.nextInt();
        System.out.println("请输入0或1来决定输出方式  1代表输出到文件,0代表输出到打印机: ");      
        p=in.nextInt();
    }

3. Generate random integer function:

//生成随机整数
public static int CreateZSJS() {
        Random r=new Random();
        int x=r.nextInt(MaxNumber);
        return x;
    }

4. Generate random floating point number function:

//生成随机浮点数
    public static float CreateXSJS() {
        Random r=new Random();
        float x=r.nextFloat();
        return x;
    }

5. Generate a random sign function:

public static char CreateFH(){
        Random r=new Random();
        int x=r.nextInt(3);                                         
        char y =0;
        if(f==0&&x==0)
             y = '+';
        if(f==0&&x==1)
            y='-';
        if(f==1&&x==0)
            y='+';
        if(f==1&&x==1)
            y='-';
        if(f==1&&x==2)
            y='*';
        if(f==1&&x==3)
            y='/';
        if(y==0)
        {
            return CreateFH();
        }
        else
            return y;
        }

6. Generate the expression function and the main function:

    //生成运算式
    public static void main(String[] args) {
        StringBuilder bf=new StringBuilder();                   //创建个字符串生成器
        USERSet();
        int i;
        for(i=0;i<TMNumber;i++)
        {
            bf.append(i+1);
            bf.append(':');
            bf.append('\t');
            if(k==0)
            {
                if(x==0)
                {
                    bf.append(CreateZSJS());
                    bf.append( CreateFH() );
                    bf.append( CreateZSJS() );
                    bf.append( CreateFH() );
                    bf.append( CreateZSJS() );
                    bf.append('=');
                    bf.append('\n');
                }
                else
                {
                    bf.append( CreateXSJS() );
                    bf.append( CreateFH() );
                    bf.append( CreateXSJS() );
                    bf.append( CreateFH() );
                    bf.append( CreateXSJS() );
                    bf.append('=');
                    bf.append('\n');
                }
            }
            else
            {
                Random r=new Random();
                int j=r.nextInt(2);
                if(j==0)
                {
                    if(x==0)
                    {
                        bf.append( '(' );
                        bf.append( CreateZSJS() );
                        bf.append( CreateFH() );
                        bf.append( CreateZSJS() );
                        bf.append( ')' );
                        bf.append( CreateFH() );
                        bf.append( CreateZSJS() );
                        bf.append('=');
                        bf.append('\n');
                    }
                    else
                    {
                        bf.append( '(' );
                        bf.append( CreateXSJS() );
                        bf.append( CreateFH() );
                        bf.append( CreateXSJS() );
                        bf.append( ')' );
                        bf.append( CreateFH() );
                        bf.append( CreateXSJS() );
                        bf.append('=');
                        bf.append('\n');
                    }
                }
                if(j==1)
                {
                    if(x==0)
                    {
                        bf.append( CreateZSJS() );
                        bf.append( CreateFH() );
                        bf.append( '(' );
                        bf.append( CreateZSJS() );
                        bf.append( CreateFH() );
                        bf.append( CreateZSJS() );
                        bf.append( ')' );
                        bf.append('=');
                        bf.append('\n');
                    }
                    else
                    {
                        bf.append( CreateXSJS() );
                        bf.append( CreateFH() );
                        bf.append( '(' );
                        bf.append( CreateXSJS() );
                        bf.append( CreateFH() );
                        bf.append( CreateXSJS() );
                        bf.append( ')' );
                        bf.append('=');
                        bf.append('\n');
                    }
                }
            }
        }
        if(p==0)
        {
            System.out.println(bf.toString());
        }
        else
        {
            File file = new File("d:/SZYS.txt");
            try {                                                                               //将结果输入到文件中
                FileOutputStream out = new FileOutputStream(file);
                String s=bf.toString();
                out.write(s.getBytes());
                out.close();
            } catch(Exception e){
                e.printStackTrace();
            }
            try {                                                                               //将结果从文件中输出
                FileInputStream in = new FileInputStream(file);
                byte byt[] = new byte[1024];
                int len = in.read(byt);
                System.out.println("文件中信息:" +'\n'+ new String(byt,0,len));
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Navigator - Bai Lu's blog address

code address

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324395764&siteId=291194637