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();
        }
    }
}

三、截图

 

 

 

转载于:https://www.cnblogs.com/wuren-best/p/11530373.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值