//注:这个程序还没有实现的地方为分数的计算方法未能实现,只是简单的两个数运算,没有实现多个数,四则运算中的数没有涉及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. ARM核心板_迅为4418核心板_高稳定超轻薄_研发超灵感

    ARM核心板_迅为4418核心板_三星四核S5P4418处理器 4418核心板正面: 4418核心板反面:4418核心板尺寸图:详情了解:https://item.taobao.com/item.ht ...

  2. 为什么可逆矩阵又叫“非奇异矩阵(non-singular matrix)”?

    最近在捡回之前的线性代数知识,在复习可逆矩阵的时候,发现有的书上把可逆矩阵又称为非奇异矩阵,乍一看名字完全不知所云,仔细一分析,还是不明白.要想弄明白,还是得从英文入手,下面的解释主要从这里得来的Wh ...

  3. centos设置服务开机启动失败问题

    1.安装某服务设置开机启动的时候报错 [root@node1 ~]# systemctl enable lvm2-lvmetad.serviceThe unit files have no [Inst ...

  4. java 八种基本数据类型

  5. CF1091E New Year and the Acquaintance Estimation

    题目地址:CF1091E New Year and the Acquaintance Estimation 首先,易知 \(ans\) 的奇偶性与所有给出的数的和的奇偶性相同 其次,易证 \(ans\ ...

  6. 【转】MySQL— 基础

    [转]MySQL— 基础 目录 一.MySQL概述 二.下载安装 三.数据库操作 四.数据表操作 五.表内容操作 一.MySQL概述 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司 ...

  7. ES6学习笔记三(proxy和reflect)

    proxy用法 // 代理 { let obj={ time:'2017-03-11', name:'net', _r: }; let monitor=new Proxy(obj,{ // 拦截对象属 ...

  8. hibernate框架学习之对象状态

    lHibernate对象共有三种状态 •瞬时状态:瞬时对象 •持久化状态:持久化对象 •托管状态:托管对象 l瞬时对象(Transient Object),简称TO l瞬时对象指的是应用程序创建出来的 ...

  9. Holedox Eating HDU - 4302 2012多校C 二分查找+树状数组/线段树优化

    题意 一个长度$n<=1e5$的数轴,$m<=1e5$个操作 有两种一些操作 $0$  $x$ 在$x$放一个食物 $1$ 一个虫子去吃最近的食物,如果有两个食物一样近,不转变方向的去吃 ...

  10. hostapd中扫描的调用过程

    扫描结果获取supplicant_event(EVENT_SCAN_RESULTS)wpa_supplicant_event_scan_results_wpa_supplicant_event_sca ...