JAVA课堂随机出题

一、设计思路

  1、利用随机数来确定两个数字。

  2、生成0-4的随机数,分别代表 加 减 乘 除。

  3、输入的题数利用for循环来出题,每行输出几道题便在循环中加入if语句,当前出题数与每行输出题数求余为0时,换行。

  4、利用三个数组存信息,分别存出过的题的两个数字和运算符号。

  5、循环开始后,先随机数生成两个数字和运算符号,然后再与数组中的信息进行比对,如果是三个信息都不相等的话进行下一步操作,否则再次生成三个信息。

  6、输出相应的信息,每隔相应数量个式子便换行。

二、源代码

package 课堂;
import java.math.*;
import java.util.*;
public class ceshi {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int sum=scanner.nextInt();
        int hang=scanner.nextInt();
        int[] a= new int[sum];
        int[] b= new int[sum];
        int[] c= new int[sum];
        for(int i=0;i<sum;i++) 
        {
        int x = (int)(Math.random()*99+1);
        int y = (int)(Math.random()*99+1);
        int choose=(int)(Math.random()*4);
        if(choose==1||choose==3)
        {
            while(x<y)
            {
                 x = (int)(Math.random()*99+1);
                 y = (int)(Math.random()*99+1);    
            }
        }
        if(choose==2)
        {
            
            while(x*y>100)
            {
                 x = (int)(Math.random()*99+1);
                 y = (int)(Math.random()*99+1);    
            }
        }
        if(choose==3)
        {
            while(x%y!=0)
            {
                 x = (int)(Math.random()*99+1);
                 y = (int)(Math.random()*99+1);    
            }
        }
        boolean pd=true;
        if(i!=0) {
        while(pd) {
        for(int cha=0;cha<i;cha++)
        {
            if(x!=a[i]&&y!=b[i]&&choose!=c[i])
            {
                pd=false;
                break;
            }        
        }
            x = (int)(Math.random()*99+1);
            y = (int)(Math.random()*99+1);
             choose=(int)(Math.random()*4);
        if(choose==1||choose==3)
        {
            while(x<y)
            {
                 x = (int)(Math.random()*99+1);
                 y = (int)(Math.random()*99+1);    
            }
        }
        if(choose==2)
        {
            
            while(x*y>100)
            {
                 x = (int)(Math.random()*99+1);
                 y = (int)(Math.random()*99+1);    
            }
        }
        if(choose==3)
        {
            while(x%y!=0)
            {
                 x = (int)(Math.random()*99+1);
                 y = (int)(Math.random()*99+1);    
            }
        }
        }
        }
        a[i]=x;
        b[i]=y;
        c[i]=choose;
        if(choose==0)
        {
            System.out.print((i+1)+"题为:"+x+"+"+y+"=    ");
        }
        if(choose==1)
        {
            System.out.print((i+1)+"题为:"+x+"-"+y+"=    ");
        }
        if(choose==2)
        {
            System.out.print((i+1)+"题为:"+x+"*"+y+"=    ");
        }
        if(choose==3)
        {
            System.out.print((i+1)+"题为:"+x+"/"+y+"=    ");
        }
        if((i+1)%hang==0)
        System.out.println();
        }
    }
}

三、截图

 

猜你喜欢

转载自www.cnblogs.com/wuren-best/p/11530373.html