你的位置:首页 > Java教程

[Java教程]小学生四则运算出题程序 无操作界面java版 简单的运用java中一些基本操作


这是本学期java课中实验大纲里的第一个实验,这里简单做了一个无用户界面版本。

import java.util.Random;import java.util.Scanner;public class MathTest {  public static void main(String[] args) {    // TODO Auto-generated method stub        double right = 0;    double wrong = 0;    /*Scanner sc2 = new Scanner(System.in);    int num2 = sc2.nextInt();*/    for (int i = 0; i < 10; i++) {      System.out.println("第"+(i+1)+"道题,请在下方输入答案:");              //生成两个随机数    Random r1 = new Random();    int x = r1.nextInt(10)+1;    Random r2 = new Random();    int y = r2.nextInt(10)+1;    //生成随机运算符  + - * /    Random r3 = new Random();    int z = r3.nextInt(4);    char[] chs = {'+','-','*','/'};    String Operator = String.valueOf(chs[z]);        //生成题目    if(Operator.equals("+")){      System.out.println(x+"+"+y+"=");      boolean b = add(x,y);      if(b == true){        right++;System.out.println("bingo! ");      }else{        wrong++;System.out.println("oh~no ");      }      System.out.println("the right answer: "+(x+y));          }else if(Operator.equals("-")){      System.out.println(x+"-"+y+"=");      boolean b =minus(x,y);      if(b == true){        right++;System.out.println("bingo! ");      }else{        wrong++;System.out.println("oh~no ");      }      System.out.println("the right answer: "+(x-y));          }else if(Operator.equals("*")){      System.out.println(x+"×"+y+"=");      boolean b =times(x,y);      if(b == true){        right++;System.out.println("bingo! ");      }else{        wrong++;System.out.println("oh~no ");      }      System.out.println("the right answer: "+(x*y));    }else{      System.out.println(x+"÷"+y+"=");      boolean b =divide(x,y);      if(b == true){        right++;System.out.println("bingo! ");      }else{        wrong++;System.out.println("oh~no ");      }       System.out.println("the right answer: "+((float)x /(float) y));    }    System.out.println("-------------------------------");    }    System.out.println("您一共做对了"+right+"道题.");    System.out.println("您一共做错了"+wrong+"道题.");    if(wrong > 0){    System.out.println("正确率为"+(right/(wrong+right))*100+"%");    }else{      System.out.println("没法除了啊  。。。。");    }  }  private static boolean add(int x,int y) {    // TODO Auto-generated method stub    Scanner sc = new Scanner(System.in);    int num1 = sc.nextInt();    int result = x + y;    if(num1 == result){      return true;    }else{      return false;    }      }  private static boolean minus(int x,int y) {    // TODO Auto-generated method stub    Scanner sc = new Scanner(System.in);    int num1 = sc.nextInt();    int result = x - y;    if(num1 == result){      return true;    }else{      return false;    }      }  private static boolean times (int x,int y) {    // TODO Auto-generated method stub    Scanner sc = new Scanner(System.in);    int num1 = sc.nextInt();    int result = x * y;    if(num1 == result){      return true;    }else{      return false;    }    }  private static boolean divide(int x,int y) {    // TODO Auto-generated method stub    Scanner sc = new Scanner(System.in);    float num1 = sc.nextFloat();    float result =(float)x /(float) y;    if(num1 == result){      return true;    }else{      return false;    }  }}

能看到判断对错的方法运用了直接运算符计算结果与函数定义后的运算结果相比较,相等得分,不相等则不得分。

编程中出现的问题:在计算结果为小数的除法时,一开始会出现结果怎么算都不对的情况,他的正确答案也是一个向偶舍入的数值,那肯定是不可取的

解决方法:原因是x和y默认int型,在float除法中要将两者都强制类型转换。【这样他的精度为小数点后7位】

附上最后的一部分结果展示。