四则运算2.0

package myproject2;
import java.util.*;
import java.lang.Math;
public class RandomCommon {
	public static char operator(int n,int se) {
		char op=' ';
		Random random = new Random();
		n = random.nextInt(100);
		if(se==1) 
		{
			switch(n%4)
			{
			case 0:op = '+';break;
			case 1:op = '-';break;
			case 2:op = '*';break;
			case 3:op = '/';break;
			}
		}
		if(se==2)
		{
			switch(n%2)
			{
			case 0:op = '+';break;
			case 1:op = '-';break;
			}
		}
		return op;
	}
	public static String chachong(String str,int min,int max,int sumoperand,int chengchu)
	{
		String []arr = new String[1000];
		int count=0;
		arr[count]=problem( str, min, max, sumoperand, chengchu);
		if(count==0)
		{
			count++;
			return arr[0];
		}
		else
		{
			boolean flag = true;
		    for (int j = 0; j <count; j++) 
		    {
		                if( arr[count]== arr[j]) flag = false;
		    }
		    if(flag)
		    {
	            count++;
	            return arr[count];
	        }
		    else
		    {
		    	return chachong(str,min,max,sumoperand,chengchu);
		    }
		}


	}
	public static void main(String[] args) {
		int sumtitle;//出题个数
		int sumoperand;//操作数个数
		int min,max;
		int chengchu;
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入出题个数:");
		sumtitle = sc.nextInt();
		System.out.println("请输入操作数个数:");
		sumoperand = sc.nextInt();
		System.out.println("请输入任意数范围:");
		System.out.println("min=");
		min = sc.nextInt();
		System.out.println("max=");
		max = sc.nextInt();
		System.out.println("是否需要乘除法:1.需要 2.不需要");
		chengchu = sc.nextInt();
		sc.close();
		String[] str = new String[sumtitle];
		String str0;
		Random random = new Random();
		for(int i=0;i<sumtitle;i++)
		{
			int a = (int)(Math.random()*(max-min)+min);
			int b = (int)(Math.random()*(max-min)+min);
			int cc = random.nextInt(100);
			char c = operator(cc,chengchu);
			str0 = ""+a+c+b;
			str[i] = problem(str0,min,max,sumoperand,chengchu);
		}
		for(int i=0;i<sumtitle;i++)
		{
			System.out.println(str[i]+"\n");
		}
	}
	public static String problem(String str,int min,int max,int sumoperand,int chengchu) {
		Random random = new Random();
		int b = random.nextInt(100);
		for(int i=2;i<sumoperand;i++)
		{
			int addnumber = (int)(Math.random()*(max-min)+min);
			char c = operator(b,chengchu);
			Random r=new Random();
			int kuohao=r.nextInt(99);
			switch(kuohao%2)
			{
			case 0:str ="("+str+c+addnumber+")";break;
			case 1:str=str+c+addnumber;break;
			}
			
		}
		return str;
	}
	
	
}

 

posted @ 2021-09-25 22:30  Lindseyyip  阅读(17)  评论(0编辑  收藏  举报