像阿超那样,花20分钟写一个能自动生成小心四则运算题目的 “软件”,要求:除了整数以外,还要支持真分数的四则运算。 和同学们比较一下各自程序的功能,实现方法的异同,等等...
package chapter;
public class szys {
?? ?public static void main(String[] args)
??? {
??????? print30Questions();
??? }
?? ?
??? private static void print30Questions()
??? {
??????? //說明:打印30道題函數(shù),把接收到的題目字符串按照指定格式輸出。
??????? for (int i = 0; i < 10; i++)
??????? {
??????????? System.out.print( i+1 );
??????????? System.out.print("." + getPfQuestion() + "??? ");
??????????? System.out.print( i+11 );
??????????? System.out.print("." + getPfQuestion() + "??? ");
??????????? System.out.print( i+21 );
??????????? System.out.println("." + getPfQuestion());
??????? }
??? }
??? private static String getPfQuestion()
??? {
??????? //說明:getPfQuestion()得到真分數(shù)題目函數(shù),隨機生成真分數(shù)題目,返回真分數(shù)題目字符串。
??????? int t = 0;
??????? String strz = "";?? ?
?????? ?
??????? int x1=1+(int)(Math.random()*10);
??????? int x2=1+(int)(Math.random()*10);//分母
?????? ?
??????? int y1=1+(int)(Math.random()*10);
??????? int y2=1+(int)(Math.random()*10);//分母
?????? ?
??????? if( x2 <= x1 )
??????? {
??????????? t = x2;
??????????? x2 = x1;
??????????? x1 = t;
??????????? if( x1 == x2 )
??????????? {
??????????????? x1 = x1 - 1;//保證分子比分母小
??????????? }
??????? }
?????? ?
??????? if( y2 <= y1 )
??????? {
??????????? t = y2;
??????????? y2 = y1;
??????????? y1 = t;
??????????? if( y1 == y2 )
??????????? {
??????????????? y1 = y1 - 1;//保證分子比分母小
??????????? }
??????? }
?????? ?
??????? int z=1+(int)(Math.random()*100);
?????? ?
??????? if( z<=25 )
??????? {
??????????? strz = "×";
??????? }
??????? if( z>25 && z<=50 )
??????? {
??????????? strz = "÷";
??????????? if( y1 == 0 )
??????????? y1 = y1 + 1;//除數(shù)的分子不能為0是不是
??????? }
??????? if( z>50 && z<=75)
??????? {
??????????? strz = "+";?? ?
??????? }
??????? if( z>75 )
??????? {
??????????? strz = "-";
??????????? if( x1/x2 > y1/y2 )
??????????? {
??????????????? t = x1;
??????????????? x1 = y1;
??????????????? y1 = t;
?????????????? ?
??????????????? t = x2;
??????????????? x2 = y2;
??????????????? y2 = t;//保證減法是大數(shù)減去小數(shù)
??????????? }
??????? }?????????? ?
??????? String strx1 = String.valueOf( x1 );
??????? String strx2 = String.valueOf( x2 );
??????? String stry1 = String.valueOf( y1 );
??????? String stry2 = String.valueOf( y2 );
??????? String ques = " " + strx1 + "/" + strx2 + strz + stry1 + "/" + stry2 + "=";
??????? return ques;
??? }
}
轉(zhuǎn)載于:https://www.cnblogs.com/zxb2078915108/p/6055858.html
總結(jié)
以上是生活随笔為你收集整理的像阿超那样,花20分钟写一个能自动生成小心四则运算题目的 “软件”,要求:除了整数以外,还要支持真分数的四则运算。 和同学们比较一下各自程序的功能,实现方法的异同,等等...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 将对象转换为JSON字符串
- 下一篇: Php真太阳时计算公式,李顺祥:真太阳时