300道随机四则运算小程序(java编写)
这是由Java编写的300道随机四则运算小程序, 运算数范围为0~100(不包括100),运算结果保留两位小数。
程序代码:
import java.util.*; class Yunsuan{ public static void main(String[] args){ Random rand = new Random(); for(int i = 0; i < 300; i++){ int num1 = (int)(rand.nextDouble() * 100); int num2 = (int)(rand.nextDouble() * 100); double num3 = 0; char ch = 0; switch(rand.nextInt(4)){ case 0: ch = '+'; num3 = 1.0 * num1 + num2; break; case 1: ch = '-'; num3 = 1.0 * num1 - num2; break; case 2: ch = '×'; num3 = 1.0 * num1 * num2; break; case 3: ch = '÷'; while(num2 == 0){num2 = (int)(rand.nextDouble() * 100);} num3 = 1.0 * num1 / num2; break; } num3 *= 100; num3 = Math.round(num3); num3 /= 100; if((num3 - (int)num3) == 0){ System.out.println(num1 + "" + ch + num2 + "=" + (int)num3); } else{ System.out.println(num1 + "" + ch + num2 + "=" + num3); } } } }
相关推荐
zhiliang 2020-01-31
roseying 2019-12-01
帕尼尼 2014-01-13
星愿 2019-11-16
85397518 2019-11-16
getianao 2019-11-16
yboker 2019-06-30
hyv 2014-01-13
袁萌 2017-10-01
xiaohao 2011-04-06
drilistbox 2019-04-25
pythoncream 2016-02-27
LHpython 2019-04-08
吴家应 2014-03-10
PHP100 2019-03-28
PHP100 2019-03-27
明学的白板 2018-05-20