Java编写一个随机产生小学生四则运算题30道
//注:这个程序还没有实现的地方为分数的计算方法未能实现,只是简单的两个数运算,没有实现多个数,四则运算中的数没有涉及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道的更多相关文章
- 30道小学生四则运算题C/C++编程
软件工程科课上,老师通过实例讲解什么是程序,程序和软件的区别,要求我们通过短时间写一道编程题, 题目就是编写30道小学生四则运算题.以下就是源代码: #include<iostream.h> ...
- 使用Java编写一个简单的Web的监控系统cpu利用率,cpu温度,总内存大小
原文:http://www.jb51.net/article/75002.htm 这篇文章主要介绍了使用Java编写一个简单的Web的监控系统的例子,并且将重要信息转为XML通过网页前端显示,非常之实 ...
- 软件工程课后作业——用JAVA编写的随机产生30道四则运算
package com.java.sizeyunsuan; public class lianxi { String f() { int i=(int)(Math.random()*10); int ...
- 面试题之java 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 要求不能出现截半的情况
题目:10. 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串. 但是要保证汉字不被截半个,如"我ABC"4,应该截为"我AB",输 ...
- java,编写一个从1循环到150并在每行打印一个值,另外在每个3的倍数行上打印出foo,在每个5的倍数行上打印biz,在每个7的倍数上打印baz.
需求:编写一个从1循环到150并在每行打印一个值,另外在每个3的倍数行上打印出foo,在每个5的倍数行上打印biz,在每个7的倍数上打印baz. package study01; public cla ...
- java面试题及答案(基础题122道,代码题19道)
JAVA相关基础知识 1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分, ...
- 转 java面试题及答案(基础题122道,代码题19道)
JAVA相关基础知识1.面向对象的特征有哪些方面 1.抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时 ...
- 用C++编写一个随机产生多个两位数四则运算式子的简单程序
一 设计思想: 1.首先可以想到一个四则运算式子的组成:两个运算数和一个运算符: 2.两个运算数的随机由调用随机函数产生,其中可以设定运算数的范围: 3.一个运算符的随机产生可以分为加减乘除四种情况, ...
- java编写一个可以上下移动的小球:运行后,可以通过上下左右键进行移动
/* * 功能:加深对事件处理机制的理解 * 1.通过控制上下左右键,来控制一个小球的位置 */package com.test1;import java.awt.*;import javax.swi ...
随机推荐
- phpcms中set_config和get_sysinfo函数
/** * 设置config文件 * @param $config 配属信息 * @param $filename 要配置的文件名称 */ function set_config($config, $ ...
- 小程序前端防止重复点击请求api的简陋方法
upload: function () { let that = this; let {uploadFlag} = that.data; if (that.data.uploadFlag) { ret ...
- 第25月第22日 django channels
1. https://github.com/andrewgodwin/channels-examples/ https://channels.readthedocs.io/en/latest/
- 第19月第17天 uitextview 文本垂直居中 uiimage中间不拉伸
1. open class VericalCenteringScrollView: UIScrollView { override open var contentOffset: CGPoint { ...
- linux 普通用户下使用 jdk 、Tomcat
需求: 在已经跑的 1.7java环境中需要跑1.8java环境所需要的tomcat,那么因为java环境的不同,nginx代理会出现很多问题,tomcat根本跑不起来,所以提供了以下解决方案. 一, ...
- Kaldi中的Chain模型
Chain模型的训练流程 链式模型的训练过程是MMI的无网格的版本,从音素级解码图生成HMM,对其使用前向后向算法,获得分母状态后验,通过类似的方式计算分子状态后验,但限于对应于转录的序列. 对于神经 ...
- CSS公共清除浏览器默认样式
/*Vue隐藏*/ [v-cloak] { display: none; } /*清除样式*/ body, ol, ul, dl, li, dt, dd, h1, h2, h3, h4, h5, h6 ...
- K - Subarrays OR Gym - 102152K (思维)
题目链接: K - Subarrays OR Gym - 102152K 题目大意:T组测试样例,然后n个数,让你求每一个l,r中有多少个不同的异或值. 具体思路: 对于(1,i)这个区间, 我们当前 ...
- MySql cmd下的学习笔记 —— 有关select的操作(in, and, where, like等等)
为方便本节学习, 请先自行建立本表: 建一个商品表: create table goods ( -> goods_id mediumint(8) unsigned not null auto_i ...
- w10谷歌chrome关闭自动更新
运行输入:msconfig打开服务 选择服务,找到谷歌更新 ,点击禁用 ,然后保存 保存会要求重启电脑 ,重启后打开页面谷歌 ,会出现弹窗,是否更新 ,点否 . 然后解决,不会再自动更新了. 这是 ...