第四次作业

package CSniu;

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

public class Calculate
{
static Random random = new Random();
Scanner scanner = new Scanner(System.in);
int x, y;
static int answer;
static int right;
static int fault;
static int time;
public static void main(String[] args)
{
Calculate calculate = new Calculate();
Scanner scanner = new Scanner(System.in);
System.out.print("请选择运算符。1加,2减,3乘,4除。");
int select = scanner.nextInt();
System.out.print("请规定出题个数。");
time = scanner.nextInt();
System.out.print("是否混合运算。0:是。1:否");
int g = scanner.nextInt();
if (g == 0)
{
hunHe();
}
switch (select)
{
case 1:
for (int i = 0; i < time; i++)
{
calculate.add();
}
System.out.print("你作对了" + calculate.right + "道,错了" + calculate.fault + "道。");
break;
case 2:
for (int i = 0; i < time; i++)
{
calculate.sub();
}
System.out.print("你作对了" + calculate.right + "道,错了" + calculate.fault + "道。");
break;
case 3:
for (int i = 0; i < time; i++)
{
calculate.mul();
}
System.out.print("你作对了" + calculate.right + "道,错了" + calculate.fault + "道。");
break;
case 4:
for (int i = 0; i < time; i++)
{
calculate.div();
}
System.out.print("你作对了" + calculate.right + "道,错了" + calculate.fault + "道。");
break;
default:
break;
}
}

void randNumber()
{
x = random.nextInt(11);
y = random.nextInt(11);
}

void add()
{
randNumber();
System.out.println("请输入 " + x + "+" + y + " 的正确答案:");
while (true)
{

try
{
Scanner scanner = new Scanner(System.in);
answer = scanner.nextInt();
break;
}
catch (Exception e)
{
System.out.println("請輸入一個整數。");
}
}
if (answer != (x + y))
{
fault++;
}
else
{
right++;
}
}

void sub()
{
randNumber();
if (x < y)
{
System.out.print("请输入 " + y + "-" + x + " 的正确答案:");
answer = scanner.nextInt();
if (answer != (y - x))
{
fault++;
}
else
{
right++;
}
}
System.out.println("请输入 " + x + "-" + y + " 的正确答案:");
while (true)
{

try
{
Scanner scanner = new Scanner(System.in);
answer = scanner.nextInt();
break;
}
catch (Exception e)
{
System.out.println("請輸入一個整數。");
}
}
if (answer != (x - y))
{
fault++;
}
else
{
right++;
}
}

void mul()
{
randNumber();
System.out.println("请输入 " + x + "*" + y + " 的正确答案:");
while (true)
{

try
{
Scanner scanner = new Scanner(System.in);
answer = scanner.nextInt();
break;
}
catch (Exception e)
{
System.out.println("請輸入一個整數。");
}
}
if (answer != (x * y))
{
fault++;
}
else
{
right++;
}
}

void div()
{

randNumber();
if (x == 0)
{
System.out.print("请输入 " + y + "/" + x + " 的正确答案:");
answer = scanner.nextInt();
if (answer != (y / x))
{
fault++;
}
else
{
right++;
}
}
System.out.println("请输入 " + x + "/" + y + " 的正确答案:");
while (true)
{

try
{
Scanner scanner = new Scanner(System.in);
answer = scanner.nextInt();
break;
}
catch (Exception e)
{
System.out.println("請輸入一個整數。");
}
}
if (answer != (x / y))
{
fault++;
}
else
{
right++;
}
}

public static void hunHe()
{
Scanner scanner = new Scanner(System.in);
int x;
int y;
int z;
int s = random.nextInt(4);
for (int i = 0; i < time; i++)
{
x = random.nextInt(11);
y = random.nextInt(11);
z = random.nextInt(11);
switch (s)
{
case 0:

System.out.println("请输入" + x + "+" + "(" + y + "*" + z + ")" + "的结果;");
answer = scanner.nextInt();
if (answer != x + (y * z))
{
fault++;
}
else
{
right++;
}
break;

case 1:
if (y == 0)
{
System.out.println("请输入" + x + "+" + "(" + z + "/" + y + ")" + "的结果;");
answer = scanner.nextInt();
if (answer != x + (z / y))
{
fault++;
}
else
{
right++;
}
break;
}
System.out.println("请输入" + x + "+" + "(" + y + "/" + z + ")" + "的结果;");
answer = scanner.nextInt();
if (answer != x + (y / z))
{
fault++;
}
else
{
right++;
}
break;
case 2:
if (x < (y * z))
{
System.out.println("请输入" + "(" + y + "*" + z + ")" + "-" + x + "的结果;");
answer = scanner.nextInt();
if (answer != (y * z) - x)
{
fault++;
}
else
{ right++;

}
break;
}
System.out.println("请输入" + x + "-" + "(" + y + "*" + z + ")" + "的结果;");
answer = scanner.nextInt();
if (answer != x - (y * z))
{
fault++;
}
else
{
right++;
}
break;
case 3:
if (y == 0)
{
System.out.println("请输入" + x + "-" + "(" + z + "/" + y + ")" + "的结果;");
answer = scanner.nextInt();
if (answer != x + (z / y))
{
fault++;
}
else
{
right++;
break;
}
System.out.println("请输入" + x + "-" + "(" + y + "/" + z + ")" + "的结果;");
answer = scanner.nextInt();
if (answer != x - (y / z))
{
fault++;
}
else
{
right++;
}
}
}
}
}
}

我的结对编程对象是王贺!

 

 

 

posted @ 2015-10-26 16:08  胡蝶  阅读(120)  评论(0编辑  收藏  举报