JAVA class random topic

First, the design ideas

  1, two numbers is determined using a random number.

  2 generates a random number from 0 to 4, representing Math.

  3, the title number input by a theme for loop, each line of output will be added to several questions if statement in the loop, the number of questions and the current question number per row output a remainder is 0, the line feed.

  4, information is stored using three arrays, respectively through the two stored numbers and operators question.

  5, after the start of the cycle, the first random number generating two numbers and operators, and then to compare the information with the array, three pieces of information if it is not equal, then the next step, otherwise generate three information again.

  6, outputs the corresponding information, every two equations will be a corresponding number of wrap.

Second, the source code

 

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

Third, screenshots

 

 

 

Guess you like

Origin www.cnblogs.com/wuren-best/p/11530373.html