程序设计思想
先生成两个1-100的随机数表示两个运算数,然后生成0-3之间的随机整数0,1,2,3分别跟+,-,*,/对应,然后判断运算结果是否小于100,其中减法运算结果要大于0
import java.util.Random; import java.util.Scanner; public class Max { public static void main(String[] args) { Random random =new Random(); char operator[]= {'+','-','*','/'}; int num1=0; int num2=0; int Operator; int num=0; Scanner in=new Scanner(System.in); System.out.println("请输入题目数"); if(in.hasNextInt()) { num=in.nextInt(); } for(int i = 0 ;i < num ;) { num1=(int)(Math.random()*100)+1; num2=(int)(Math.random()*100)+1; Operator=random.nextInt(4); if(Operator==0) { if((num1+num2)<=100)//加法运算结果小于100 { System.out.print(num1+"+"+num2+"= "); i++; if(i%5==0&&i>=5)//五个表达式换行 { System.out.println(); } } } else if(Operator==1) { if((num1-num2)<=100&&(num1-num2)>0)//减法运算结果小于100并且大于0 { System.out.print(num1+"-"+num2+"= "); i++; if(i%5==0&&i>=5)//五个表达式换行 { System.out.println(); } } } else if(Operator==2) { if((num1*num2)<=100)//乘法运算结果小于100 { System.out.print(num1+"*"+num2+"= "); i++; if(i%5==0&&i>=5)//五个表达式换行 { System.out.println(); } } } else if(Operator==3) { if((num1>0)&&(num2>0))//除法运算结果小于100并且能整除 { System.out.print(num1+"/"+num2+"= "); i++; if(i%5==0&&i>=5)//五个表达式换行 { System.out.println(); } } } } } }
运行结果截图