码迷,mamicode.com
首页 > 编程语言 > 详细

Java编写一个随机产生小学生四则运算题30道

时间:2018-10-05 22:35:34      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:detail   question   exists   main   n+1   小数   pac   .net   csdn   

//注:这个程序还没有实现的地方为分数的计算方法未能实现,只是简单的两个数运算,没有实现多个数,四则运算中的数没有涉及0.

package 课堂测试1;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
public class Arithmetic {
String f()
{
int i=(int)(1+Math.random()*100);
int j=(int)(1+Math.random()*100);
if(i>=j)
{
int temp=i;
i=j;
j=temp;
}
return("("+i+"/"+j+")");
}//Math.random()是令系统随机选取大于等于 0.0 且小于 1.0 的伪随机 double 值
/*
* Java中Math类的random()方法可以生成[0,1)之间的随机浮点数。而double类型数据强制转换成int类型,整数部分赋值给int类型变量,小数点之后的小数部分将会丢失。如果要生成[0,n]的随机整数的话,只需要Math.random()乘以n+1,生成[0,n+1)的浮点数,再强制类型转换为int类型,只取其整数部分,即可得到[0,n]的整数。
int b=(int)(Math.random()*10);//生成[0,9]之间的随机整数。
int temp=m+(int)(Math.random()*(n+1-m)); //生成从m到n的随机整数[m,n]
int num = (int)(Math.random()*2+1)
//以上代码即设置一个随机1到3(取不到3)的变量num。
//产生一个[0,1)之间的随机数。
Math.random():
返回指定范围的随机数(m-n之间)的公式:
Math.random()*(n-m)+m;
或者
Math.random()*(n+1-m)+m*/
public static void main(String[] args)
{
try {
File file=new File("E:\\result.txt");
if(file.exists()) {file.delete();}
FileWriter fw=new FileWriter(file,true);
PrintWriter pw=new PrintWriter(fw);
//PrintWriter()的作用是为了定义流输出的位置,并且此流可以正常的存储中文,减少乱码输出。
//备注:文件流用完之后必须及时通过close方法关闭,否则会一直处于打开状态,直至程序停止,增加系统负担。
double result;
String a,b;
Arithmetic lianxi=new Arithmetic();
for(int n=0;n<30;n++)
{System.out.println("第"+(n+1)+"道");
a=lianxi.f();
b=lianxi.f();
int i=(int)(1+Math.random()*100);
int j=(int)(1+Math.random()*100);
double i1=i;
double j1=j;
String[]operator={"+","-","*","/"};
Random r=new Random();
int num=r.nextInt(4);//该方法的作用是生成一个随机的int值,该值介于[0,4)的区间,也就是0到4之间的随机int值,包含0而不包含4
int t=(int)(Math.random()*3);//分为三种情况,两个整数运算,一个整数一个分数运算,两个分数运算
//两个整数运算
if(t==0) {
if(operator[num]=="/") {
if(j==0) {
while(j==0)
j= (int)(Math.random()*100);
}//考虑除数是否为0的情况,不过用在这边没有意义,这里的j不可能为0
}
String str1=i+operator[num]+j;
if(operator[num]=="+") {result=i+j;
System.out.println(str1+"=");
pw.println(str1+"="+result);//保存到文件中去
}
else if(operator[num]=="-") {
result=i-j;
System.out.println(str1+"=");
pw.println(str1+"="+result);
}
else if(operator[num]=="*") {
result=i*j;
System.out.println(str1+"=");
pw.println(str1+"="+result);
}
else if(operator[num]=="/") {
result=i1/j1;
System.out.println(str1+"=");
pw.println(str1+"="+result);
}
}
//一个整数一个分数运算
else if(t==1) {
if(operator[num]=="/") {
if(j==0) {
while(j==0)
j= (int)(Math.random()*100);
}
}
String str2=a+operator[num]+j;
if(operator[num]=="+") {
System.out.println(str2+"=");
pw.println(str2+"=");
}
if(operator[num]=="-") {
System.out.println(str2+"=");
pw.println(str2+"=");
}
if(operator[num]=="*") {
System.out.println(str2+"=");
pw.println(str2+"=");
}
if(operator[num]=="/") {
System.out.println(str2+"=");
pw.println(str2+"=");
}
}
//两个分数运算
else if(t==2) {
String str3=a+operator[num]+b;
if(operator[num]=="+") {
System.out.println(str3+"=");
pw.println(str3+"=");
}
if(operator[num]=="-") {
System.out.println(str3+"=");
pw.println(str3+"=");
}
if(operator[num]=="*") {
System.out.println(str3+"=");
pw.println(str3+"=");
}
if(operator[num]=="/") {
System.out.println(str3+"=");
pw.println(str3+"=");
}
}
}
pw.flush();
pw.close();
fw.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
/*参考链接
https://blog.csdn.net/qq_36868342/article/details/73478112
https://blog.csdn.net/qq_21808961/article/details/79931087
https://www.cnblogs.com/xiaotiaosi/p/6394147.html
https://zhidao.baidu.com/question/417476227.html?word=printwriter&ms=1&rid=9823837345489847717
https://blog.csdn.net/duncandavid/article/details/60871080
*/

 

Java编写一个随机产生小学生四则运算题30道

标签:detail   question   exists   main   n+1   小数   pac   .net   csdn   

原文地址:https://www.cnblogs.com/zzstdruan1707-4/p/9746019.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!