//注:这个程序还没有实现的地方为分数的计算方法未能实现,只是简单的两个数运算,没有实现多个数,四则运算中的数没有涉及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道的更多相关文章

  1. 30道小学生四则运算题C/C++编程

    软件工程科课上,老师通过实例讲解什么是程序,程序和软件的区别,要求我们通过短时间写一道编程题, 题目就是编写30道小学生四则运算题.以下就是源代码: #include<iostream.h> ...

  2. 使用Java编写一个简单的Web的监控系统cpu利用率,cpu温度,总内存大小

    原文:http://www.jb51.net/article/75002.htm 这篇文章主要介绍了使用Java编写一个简单的Web的监控系统的例子,并且将重要信息转为XML通过网页前端显示,非常之实 ...

  3. 软件工程课后作业——用JAVA编写的随机产生30道四则运算

    package com.java.sizeyunsuan; public class lianxi { String f() { int i=(int)(Math.random()*10); int ...

  4. 面试题之java 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 要求不能出现截半的情况

    题目:10. 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串. 但是要保证汉字不被截半个,如"我ABC"4,应该截为"我AB",输 ...

  5. java,编写一个从1循环到150并在每行打印一个值,另外在每个3的倍数行上打印出foo,在每个5的倍数行上打印biz,在每个7的倍数上打印baz.

    需求:编写一个从1循环到150并在每行打印一个值,另外在每个3的倍数行上打印出foo,在每个5的倍数行上打印biz,在每个7的倍数上打印baz. package study01; public cla ...

  6. java面试题及答案(基础题122道,代码题19道)

    JAVA相关基础知识 1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分, ...

  7. 转 java面试题及答案(基础题122道,代码题19道)

    JAVA相关基础知识1.面向对象的特征有哪些方面 1.抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时 ...

  8. 用C++编写一个随机产生多个两位数四则运算式子的简单程序

    一 设计思想: 1.首先可以想到一个四则运算式子的组成:两个运算数和一个运算符: 2.两个运算数的随机由调用随机函数产生,其中可以设定运算数的范围: 3.一个运算符的随机产生可以分为加减乘除四种情况, ...

  9. java编写一个可以上下移动的小球:运行后,可以通过上下左右键进行移动

    /* * 功能:加深对事件处理机制的理解 * 1.通过控制上下左右键,来控制一个小球的位置 */package com.test1;import java.awt.*;import javax.swi ...

随机推荐

  1. JS/javaScript 获取div内容

    jquery: 例如<div id="abc"><a>内容</a></div>$("#abc").html(); ...

  2. Karel版本的nnet1

    除了chain,nnet1, nnet2, nnet3训练时调整转移模型,chain模型使用类似与MMI的训练准则 概要 Karel Vesely的nnet1用到以下技术: 每一层进行预训练,基于RB ...

  3. xenserver 上传centos6.8镜像

    1.宿主机操作:   # mkdir /iso # xe sr-create name-label=system-iso type=iso device-config:location=/iso de ...

  4. 深刻了解jQuery对象和普通DOM对象的区别

    深刻了解jQuery对象和普通DOM对象的区别.互相转化见Q1 Q1,js的写法:document.getElementById('save').disabled=true; 在jquery中我是这样 ...

  5. gitbash使用git 命令的准备工作

    1.git下载 2.git clone http://git.missfresh.cn/... 配置用户名密码 3.git  常用命令简写配置 git config --global alias.st ...

  6. git 配置 BeyondCompare

    安装 Beyond Compare 4 配置 git git config --global merge.tool bc3 git config --global mergetool.bc3.path ...

  7. Shiro入门 - 通过ini文件进行认证

    导入依赖 <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core ...

  8. Print Article(斜率DP入门+单调队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3507 题目大意:给你n个数,然后问你怎么分割当前的这n个数位那几组,使得每一组的权值加起来最大.每一组 ...

  9. mysql 案例 ~超时时间设置

    一 简介:今天咱们来谈谈mysql的连接时间与会话关系二 分类  mysql会话分类  1 程序连接session(最主要的)  2 binlog连接session(复制+canal消费)  3 ro ...

  10. oracle dblink的创建与使用

    一.什么是DBLINK dblink,顾名思义就是数据库的链接. 当我们要跨本地数据库访问另一个数据库中的表的数据时,在本地数据库中就必须要创建远程数据库的dblink,通过该dblink就可以达到访 ...