java自动生成加减法并检验正确_使用Java实现自动生成一百以内的加减乘除

package cc.wuque;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

while (true){

int x, y, z;

System.out.println("****欢迎使用口算练习系统****");

System.out.println("1、加法练习");

System.out.println("2、减法练习");

System.out.println("3、乘法练习");

System.out.println("4、除法练习");

System.out.print("请选择需要练习的序号:");

z = sc.nextInt();

System.out.print("请选择需要练习的次数:");

int count = sc.nextInt();

if (z == 1){

System.out.print("您选择了加法练习");

while(count != 0 ){

System.out.println("当前剩余练习次数:" + count);

x = (int) (Math.random() * 50);

y = (int) (Math.random() * 50);

System.out.print(x + "+" + y + "=");

int result = sc.nextInt();

if (result != (x + y)){

System.out.println("回答错误,正确答案:" + (x + y));

}else {

System.out.println("回答正确");

}

count--;

}

}else if (z == 2){

System.out.print("您选择了减法练习");

while (count != 0){

System.out.println("当前剩余练习次数:" + count);

x = (int) (Math.random() * 100);

y = (int) (Math.random() * 100);

while(y > x){

x = (int) (Math.random() * 100);

}

System.out.print(x + "-" + y + "=");

int result = sc.nextInt();

if (result != (x - y)){

System.out.println("回答错误,正确答案:" + (x - y));

}else {

System.out.println("回答正确");

}

count--;

}

}else if (z == 4){

System.out.println("您选择了除法练习");

System.err.println("除法的运算结果只能为整数!!!");

while (count != 0){

System.out.println("当前剩余练习次数:" + count );

x = (int) (Math.random() * 100);

y = (int) (Math.random() * 10);

if (y == 0){

y = (int) (Math.random() * 10);

}

System.out.print(x + " / " + y + "=");

System.out.println();

System.out.println();

/*String remove = String.valueOf((Integer)(x / y));*/

/*String more = String.valueOf((Integer) (x % y));*/

int remove = x / y;

int more = x % y;

String s;

Double DoubleResult = null;

int result = 0;

if (more == 0){

s = String.valueOf(remove);

result = sc.nextInt();

}else {

s = remove + "." + more;

DoubleResult = sc.nextDouble();

}

String StrResult = result + "";

String StrDoubleResult = DoubleResult + "";

/*System.out.println("result:" + result);

System.out.println("s:" + s);

System.out.println("remove:" + remove);

System.out.println("more:" + more);*/

if (s.equals(StrResult)){

System.out.println("回答正确");

}else if (s.equals(StrDoubleResult)){

System.out.println("回答正确");

}else {

System.out.println("回答错误,正确答案:" + s);

}

count--;

}

}else if (z == 3){

System.out.print("您选择了乘法练习");

while (count != 0){

System.out.println("当前剩余练习次数:" + count);

x = (int) (Math.random() * 10);

y = (int) (Math.random() * 10);

System.out.print(x + " x " + y + "=");

int result = sc.nextInt();

if (result != (x * y)){

System.out.println("回答错误,正确答案:" + (x * y));

}else {

System.out.println("回答正确");

}

count--;

}

}else {

System.out.println("您选择的序号有误或非法,请重新输入!!!! ");

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值