Java做的加减乘除出题,自动算出正确率
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
package edu.hpu.Scanner;
import java.util.Random;
import java.util.Scanner;
publicclass Test {
publicstaticvoid 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++;
}else{
wrong++;
}
}elseif(Operator.equals("-")){
System.out.println(x+"-"+y+"=");
boolean b =minus(x,y);
if(b == true){
right++;
}else{
wrong++;
}
}elseif(Operator.equals("*")){
System.out.println(x+"×"+y+"=");
boolean b =times(x,y);
if(b == true){
right++;
}else{
wrong++;
}
}else{
System.out.println(x+"÷"+y+"=");
boolean b =divide(x,y);
if(b == true){
right++;
}else{
wrong++;
} }
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("没法除了啊。。。。");
}
}
privatestaticboolean 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){
returntrue;
}else{
returnfalse;
}
}
privatestaticboolean 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){
returntrue;
}else{
returnfalse;
}
}
privatestaticboolean 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){
returntrue;
}else{
returnfalse;
}
}
privatestaticboolean divide(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){
returntrue;
}else{
returnfalse;
}
}
}