四则运算--计应192黄慧娟

需求分析:作为一名小学家长,希望软件能自动给出一组100以内的加减乘除,做出判断,以便减轻我的负担。

psp:


 

具体代码:
package szys;
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("您选择的序号有误或非法,请重新输入!!!! ");
}
}
}
}

posted @ 2021-06-01 16:27  陈十十  阅读(53)  评论(0编辑  收藏  举报