码迷,mamicode.com
首页 > 其他好文 > 详细

第二次作业

时间:2015-10-06 19:31:20      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

import java.util.Random;
import java.util.Scanner;

public class Calculator {

public static void main(String[] args) {
// TODO Auto-generated method stub
int x,y;
int option,time;
Random random=new Random();
Scanner scanner=new Scanner(System.in);
System.out.print("请输入运算符号:1+,2—,3*,4/。");
option=scanner.nextInt();
System.out.print("请输入出题个数。");
time=scanner.nextInt();
switch (option) {
case 1:
for(int i=0;i<time;i++){
x=random.nextInt(11);
y=random.nextInt(11);
Add add=new Add();
add.run(x, y);
}
System.out.println("你作对了"+Add.right+"道,错了"+Add.failed+"道。");
break;
case 2:
for(int i=0;i<time;i++){
x=random.nextInt(11);
y=random.nextInt(11);
Sub sub=new Sub();
sub.run(x, y);
}
System.out.println("你作对了"+Sub.right+"道,错了"+Sub.failed+"道。");
break;
case 3:
for(int i=0;i<time;i++){
x=random.nextInt(11);
y=random.nextInt(11);
Mul mul=new Mul();
mul.run(x, y);
}
System.out.println("你作对了"+Mul.right+"道,错了"+Mul.failed+"道。");
break;
case 4:
for(int i=0;i<time;i++){
x=random.nextInt(11);
y=random.nextInt(11);
Div div=new Div();
div.run(x, y);
}
System.out.println("你作对了"+Div.right+"道,错了"+Div.failed+"道。");
break;
default:
System.out.print("输入的选项不存在!");
break;
}
}

}

import java.util.Scanner;

public class Add {
static int answer,right,failed;
Scanner scanner=new Scanner(System.in);
public void run(int x,int y){
System.out.println("请输入 "+x+"+"+y+" 的正确答案:");
answer=scanner.nextInt();
if(answer!=(x+y)){
failed++;
}else {
right++;
}

}

}

import java.util.Scanner;

public class Div {
static int answer,right,failed;
Scanner scanner=new Scanner(System.in);
public void run(int x,int y){
System.out.println("请输入 "+x+"/"+y+" 的正确答案:");
answer=scanner.nextInt();
if(answer!=(x/y)){
failed++;
}else {
right++;
}

}
}

import java.util.Scanner;

public class Mul {
static int answer,right,failed;
Scanner scanner=new Scanner(System.in);
public void run(int x,int y){
System.out.println("请输入 "+x+"*"+y+" 的正确答案:");
answer=scanner.nextInt();
if(answer!=(x*y)){
failed++;
}else {
right++;
}

}
}

import java.util.Scanner;

public class Sub {
static int answer,right,failed;
Scanner scanner=new Scanner(System.in);
public void run(int x,int y){
System.out.println("请输入 "+x+"-"+y+" 的正确答案:");
answer=scanner.nextInt();
if(answer!=(x-y)){
failed++;
}else {
right++;
}
}
}

第二次作业

标签:

原文地址:http://www.cnblogs.com/asd123/p/4857509.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!