- 需求分析
能生成简单四则运算题目并判断用户回答对错。
能正确统计回答正确的概率。
能正确处理混合四则运算的优先级的问题。
能正确输出负数等结果。
能处理简单的加,减,乘,除运算。
能正确的处理有括号的情况并输出正确的运算结果,能正确处理输入异常的情况。 - 运行截图
-
代码实现
import java.util.;
public class sizeyunsuan {
public static void main(String[] args) {
System.out.println("输入测试题数目:");
Scanner sc=new Scanner(System.in);
int number=sc.nextInt();
double right = 0;
double wrong = 0;
for (int i = 0; i < number; i++) {
System.out.println("第"+(i+1)+"道题,请在下方输入答案:");
double op1=(int)(Math.random()10);
double op2=(int)(Math.random()10);
Random r3 = new Random();
int m = r3.nextInt(4);
char[] chs = {\'+\',\'-\',\'\',\'/\'};
String Operator = String.valueOf(chs[m]);
//生成题目
if(Operator.equals("+")){
System.out.println(op1+"+"+op2+"=");
boolean b = add(op1,op2);
if(b == true){
right++;System.out.println("right! ");
}
else{
wrong++;System.out.println("no ");
System.out.println("right answer: "+(op1+op2));
}
}
else if(Operator.equals("-")){
System.out.println(op1+"-"+op2+"=");
boolean b =minus(op1,op2);
if(b == true){
right++;System.out.println("right! ");
}
else{
wrong++;System.out.println("no ");
System.out.println("right answer: "+(op1-op2));
}
}
else if(Operator.equals("")){
System.out.println(op1+"×"+op2+"=");
boolean b =times(op1,op2);
if(b == true){
right++;System.out.println("right! ");
}
else{
wrong++;System.out.println("no ");
System.out.println("right answer: "+(op1op2));
}
}
else{
System.out.println(op1+"÷"+op2+"=");
boolean b =Div(op1,op2);
if(b == true){
right++;System.out.println("right! ");
}else{
wrong++;System.out.println("no ");
System.out.println("right answer: "+op1/op2);
}
}
}
System.out.println("做对了"+right+"道题.");
System.out.println("做错了"+wrong+"道题.");
System.out.println("正确率为"+(right/(wrong+right))*100+"%");
}
private static boolean add(double x,double y) {
Scanner sc = new Scanner(System.in);
double num1 = sc.nextDouble();
double result = x + y;
if(num1 == result){
return true;
}else{
return false;
}
}
private static boolean minus(double x,double y) {
Scanner sc = new Scanner(System.in);
double num1 = sc.nextDouble();
double result = x - y;
if(num1 == result){
return true;
}else{
return false;
}
}
private static boolean times (double x,double y) {
Scanner sc = new Scanner(System.in);
double num1 = sc.nextDouble();
double result = x * y;
if(num1 == result){
return true;
}else{
return false;
}
}
private static boolean Div(double x,double y) {
Scanner sc = new Scanner(System.in);
double num1 = sc.nextDouble();
double result =x/y;
if(num1 == result){
return true;
}else{
return false;
}
}
} -
whitestar运行图
-
码云链接
https://gitee.com/BESTI-IS-JAVA-2018/20165303WeiYujava/tree/master/src -
结对感受
通过这周的不断修改代码,不断地和小伙伴争论,终于修改出了基本实现功能的代码,然后就是实现功能,但是在运行的过程中还是有一些bug,不是太完美,由于时间紧张,本周的实验比较多,然后实现的比较粗糙,我会在以后的学习过程中继续实现这个代码。