Java编写一个随机产生小学生四则运算题30道
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java编写一个随机产生小学生四则运算题30道,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4794字,纯文字阅读大概需要7分钟。
内容图文
![Java编写一个随机产生小学生四则运算题30道](/upload/InfoBanner/zyjiaocheng/855/925411d53f7f4e6fa1498e2cb96555da.jpg)
//注:这个程序还没有实现的地方为分数的计算方法未能实现,只是简单的两个数运算,没有实现多个数,四则运算中的数没有涉及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);
}
}
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道全部内容,希望文章能够帮你解决Java编写一个随机产生小学生四则运算题30道所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。