package second;
import java.util.Random;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Test test=new Test();
test.run();
}
int x,y,select,time,rig,fail,answer;
Scanner scanner=new Scanner(System.in);
Random random=new Random();
public void run(){
System.out.print("请输入出题个数。");
time=scanner.nextInt();
System.out.print("请输入运算符号:1=“+”,2=“—”,3=“*”,4=“/”。");
select=scanner.nextInt();
switch (select) {
case 1:
add();
System.out.println("你对了"+rig+"道,错了"+fail+"道。");
break;
case 2:
sub();
System.out.println("你作对了"+rig+"道,错了"+fail+"道。");
break;
case 3:
mul();
System.out.println("你作对了"+rig+"道,错了"+fail+"道。");
break;
case 4:
div();
System.out.println("你作对了"+rig+"道,错了"+fail+"道。");
break;
default:
System.out.print("没有此运算符。");
break;
}
}
void add(){
for(int i=0;i<time;i++){
x=random.nextInt(11);
y=random.nextInt(11);
System.out.println("请输入 "+x+"+"+y+" 的正确答案:");
answer=scanner.nextInt();
if(answer!=(x+y)){
fail++;
}else {
rig++;
}
}
}
void sub(){
for(int i=0;i<time;i++){
x=random.nextInt(11);
y=random.nextInt(11);
System.out.println("请输入 "+x+"-"+y+" 的正确答案:");
answer=scanner.nextInt();
if(answer!=(x-y)){
fail++;
}else {
rig++;
}
}
}
void mul(){
for(int i=0;i<time;i++){
x=random.nextInt(11);
y=random.nextInt(11);
System.out.println("请输入 "+x+"*"+y+" 的正确答案:");
answer=scanner.nextInt();
if(answer!=(x*y)){
fail++;
}else {
rig++;
}
}
}
void div(){
for(int i=0;i<time;i++){
x=random.nextInt(11);
y=random.nextInt(11);
System.out.println("请输入 "+x+"/"+y+" 的正确答案:");
answer=scanner.nextInt();
if(answer!=(x/y)){
fail++;
}else {
rig++;
}
}
}
}
完成