四则运算代码

package myproject2;
import java.util.*;

public class sizeyunsuan {
	public static int[] randomCommon(int n,int min, int max){
       /* if (n > (max - min + 1) || max < min) {
            return null;
        }*/
        int[] result = new int[n];
        int count = 0;
        while(count < n) {
            int num = (int) (Math.random() * (max - min)) + min;
            boolean flag = true;
            for (int j = 0; j < n; j++) {
                if(num == result[j]){
                    flag = false;
                    break;
                }
            }
            if(flag){
                result[count] = num;
                count++;
            }
        }
        return result;
    }
	
public static char d(int panduan){
	Random r = new Random();
	int yunsuanfu= r.nextInt(100);
	char fh=' ';
	if(panduan==1)
	{
		switch(yunsuanfu%4)
		{
		case 0:fh='+';break;
		case 1:fh='-';break;
		case 2:fh='*';break;
		case 3:fh='/';break;
		}
	}
	else if(panduan==2)
	{
		switch(yunsuanfu%2)
		{
		case 0:fh='+';break;
		case 1:fh='-';break;
		}
	}
	return fh;
} 
public static void main(String[] args) {
	System.out.println("请输入题目个数:");
	Scanner input = new Scanner(System.in);
	int num=input.nextInt();
	System.out.println("请输入操作个数(1.两个 2.三个)");
	int czs=input.nextInt();
	System.out.println("请输入数值范围:");
	int a=input.nextInt();
	int b=input.nextInt();
	if((b-a)<num*(czs+1))
	{
		System.out.println("系统崩溃!");
		System.exit(0);
	}
	System.out.println("是否需要乘除运算(1.是 2.否)");
	int cc=input.nextInt();
	Random r = new Random();
	int x,y,z;
	if(czs==1)
	{
		int []arr1 =randomCommon(num*2,a,b);
		for(int i=0;i<num*2;i++)
		{
			x=arr1[i];
			y=arr1[i+1];
			i++;
			System.out.println(""+x+d(cc)+y+"=");
		}
		
	}
	else if(czs==2)
	{
		System.out.println("是否需要括号运算(1.是 2.否)");
		int kh=input.nextInt();
		int []arr2 =randomCommon(num*3,a,b);
		
		for(int i=0;i<num*3;i++)
		{
			x=arr2[i];
			y=arr2[i+1];
			z=arr2[i+2];
			i+=2;
			if(kh==1)
			{
				int kuohao= r.nextInt(99);
				switch(kuohao%2)
				{
				case 0:System.out.println("("+x+d(cc)+y+")"+d(cc)+z+"=");break;
				case 1:System.out.println(""+x+d(cc)+"("+y+d(cc)+z+")=");break;
				}
			}
			else if(kh==2)
			{
				System.out.println(""+x+d(cc)+y+d(cc)+z+"=");
			}
			input.close();
		}
	}
 }	
       

}

 

posted @ 2021-09-19 21:31  Lindseyyip  阅读(136)  评论(0编辑  收藏  举报