import java.util.*; public class ArithmeticTopic { private static Random random = new Random(); public ArithmeticTopic() { // TODO Auto-generated constructor stub } private String createNumber() { int number1 =1+random.nextInt(100); int point1 = 0; String numberString=""; if(random.nextBoolean()) { point1 = number1; number1 = 1+random.nextInt(point1); if(point1==number1) point1=0; } if(point1>0) { numberString = ""+number1+"/"+point1; }else { numberString = "" +number1; } return numberString; } private String topicString() { char operator = ‘+‘; switch(random.nextInt(3)) { case 0 : operator = ‘+‘; break; case 1 : operator = ‘-‘; break; case 2 : operator = ‘*‘; break; case 3 : operator = ‘/‘; break; default: break; }; return createNumber()+operator+createNumber()+"="; } public static void main(String[] args) { ArithmeticTopic topic = new ArithmeticTopic(); for(int i=0;i<10;i++) System.out.println(topic.topicString()); } }
程序思路:
产生真分数:先产生一个1-100的随机整数y, 依照随机布尔值判断是否再产生一个1-y的的随机整数x作为分子,形成x/y,如果x/y=1则直接输出x.
产生四则运算表达式: 数x1,x2,字符串中间插入随机运算符,最后加上等号输出到命令行.
时间: 2024-12-09 05:47:52