首页 技术 正文
技术 2022年11月6日
0 收藏 417 点赞 407 浏览 2550 个字

四则运算满足简单加减乘除,以及包含括号的复杂四则运算。

代码描述:

1.采用random随机数产生要参与计算的数字,以及运算符号

2.采用Scanner获取控制台输入的结果,与计算出来的结果进行比对,并告之用户。如果用户计算错误,将正确结果输出。

3.关于复杂计算,所出题是包含四种符号的混合运算。

代码展示:

random产生随机数abcd分别代表要参加运算的数字

s代表简单运算中的符号

public static void main(String[] args) {
int n = 10;
System.out.println("本次测试共10道题");// n为题目数量
Random random = new Random();
for (int o = 1; o <= n; o++) {// 产生小于等于100的随机数
int a = random.nextInt(100);
int b = random.nextInt(100);
int c = random.nextInt(100);
int d = random.nextInt(100);// 产生1 2 3 4随机数分别代表加减乘除
Random sig = new Random();
int s = sig.nextInt(4);

进行简单运算复杂运算的选择

System.out.println("~~~~~~~~~~~~~~~~~~");
System.out.println("请选择要进行的练习类型:");
System.out.println("1.简单运算;2.复杂运算");

如果选择1则为简单运算 分为加减乘数两位数的计算

                       Scanner s1 = new Scanner(System.in);
int cho = s1.nextInt();
if (cho == 1) {
// 代表符号的随机数s等于1时将进行加法运算
if (s == 1) {
int ansJia = a + b;
System.out.print(a + "+" + b + "=");
Scanner scan1 = new Scanner(System.in);
int ans = scan1.nextInt();
if (ans == ansJia) {
System.out.println("真棒!你做对了");
} else {
System.out.println("在仔细想想,你可以做对的!");
}
System.out.println("正确的结果是:" + ansJia);
}
// 代表符号的随机数s等于2时将进行减法运算
if (s == 2) {
if (a > b) {
// 为保证所得的结果为正,必须使用较大的随机数减去较小的随机数
int ansJian = a - b;
System.out.print(a + "-" + b + "=");
Scanner scan2 = new Scanner(System.in);
int ans = scan2.nextInt();
if (ans == ansJian) {
System.out.println("真棒!你做对了");
} else {
System.out.println("在仔细想想,你可以做对的!");
}
System.out.println("正确的结果是:" + ansJian);
} else {
int ansJian = b - a;
System.out.print(b + "-" + a + "=");
Scanner scan2 = new Scanner(System.in);
int ans = scan2.nextInt();
if (ans == ansJian) {
System.out.println("真棒!你做对了");
} else {
System.out.println("在仔细想想,你可以做对的!");
}
System.out.println("正确的结果是:" + ansJian);
}
}
// 乘
if (s == 3) {
int ansCheng = a * b;
System.out.print(a + "*" + b + "=");
Scanner scan3 = new Scanner(System.in);
int ans = scan3.nextInt();
if (ans == ansCheng) {
System.out.println("真棒!你做对了");
} else {
System.out.println("在仔细想想,你可以做对的!");
}
System.out.println("正确的结果是:" + ansCheng);
}
// 除
if (s == 4) {
double ansChu = (double) (Math.round(a / b));
System.out.print(a + "/" + b + "=");
Scanner scan4 = new Scanner(System.in);
int ans = scan4.nextInt();
if (ans == ansChu) {
System.out.println("真棒!你做对了");
} else {
System.out.println("在仔细想想,你可以做对的!");
}
System.out.println("正确的结果是:" + ansChu);
}}

如果选择为2 则计算四个数字的运算

else {
double ansCom = (double) (Math.round((a + b) * c / d));
System.out.println("(" + a + "+" + b + ")*" + c + "/" + d + "=");
Scanner scan5 = new Scanner(System.in);
double ans = scan5.nextDouble();
if (ans == ansCom) {
System.out.println("真棒!你做对了");
} else {
System.out.println("在仔细想想,你可以做对的!");
}
System.out.println("正确的结果是:" + ansCom);
}
}

当做题数目为10结束训练

if(o==n-1){
                System.out.println(“恭喜你完成本次训练”);
                break;
            }

运行结果如下:

week1 四则运算week1 四则运算

关于结对训练的感悟:

1.两个人一起写代码能注意到代码规范的问题,这样方便两个人看代码。

2.两个人在一起交流能减少错误的发生,自己写代可能有拼写错误自己发现不了,及时提醒在代码编写过程很重要。

3.两个人的思想一定强于一个人,所以对于问题思路更开阔。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,111
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922