第四次作业

package CSniu;

import java.util.Random;

import java.util.Scanner;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.RestoreAction;

import org.omg.CORBA.PUBLIC_MEMBER;

public class A1{ static int zq = 0;

static int cw = 0;

public static void main(String[] args) { A1 a = new A1();

System.out.print("请输入答题次数:");

Scanner w = new Scanner(System.in);

int s = w.nextInt();

System.out.println("一共" + s + "道题," + "答题完毕后自动统计正确率");

for (int i = 0; i < s; i++) { System.out.print("请输入需要的运算符:");

Scanner input = new Scanner(System.in);

String x = input.next();

System.out.print("选择混合运算吗?“是或否”");

String y = input.next();

if (y.equals("是")) { a.hunHe();

} int a1 = new Random().nextInt(10) + 1;

int a2 = new Random().nextInt(10) + 1;

switch (x) { case "+": int sum1 = a1 + a2;

System.out.print(a1 + "+" + a2 + "=");

Scanner k1 = new Scanner(System.in);

int q1;

while (true) { try { q1 = k1.nextInt();

break;

} catch (Exception e) { System.out.println("請輸入一個整數。");

}

}

if (q1 == sum1) { System.out.println("答案正确");

zq = zq + 1;

}

else { System.out.println("答案错误");

cw = cw + 1;

}

break;

case "-": int sum2 = a1 - a2;

if (a1 < a2) { int sum3 = a2 - a1;

System.out.print("请输入 " + a2 + "-" + a1 + " 的正确答案:");

int q2;

while (true) { try { Scanner scanner = new Scanner(System.in);

q2 = scanner.nextInt();

break;

} catch (Exception e) { System.out.println("請輸入一個整數。");

}

}

if (q2 == sum3) { System.out.println("答案正确");

zq = zq + 1;

}

else { System.out.println("答案错误");

cw = cw + 1;

}

break;

}

System.out.print(a1 + "-" + a2 + "=");

Scanner k2 = new Scanner(System.in);

int q2;

while (true) { try { q2 = k2.nextInt();

break;

}catch (Exception e) { System.out.println("請輸入一個整數。");

}

}

if (q2 == sum2) { System.out.println("答案正确");

zq = zq + 1;

}

else { System.out.println("答案错误");

cw = cw + 1;

}

break;

case "*":

int sum3 = a1 * a2;

System.out.print(a1 + "*" + a2 + "=");

Scanner k3 = new Scanner(System.in);

int q3;

while (true) { try { q3 = k3.nextInt();

break;

}

catch (Exception e) { System.out.println("請輸入一個整數。");

}

}

if (q3 == sum3) { System.out.println("答案正确");

cw = cw + 1;

zq = zq + 1;}

 else { System.out.println("答案错误");

cw = cw + 1;

}

break;

case "/": if (a1 == 0) { int sum4 = a2 / a1;

System.out.print(a2 + "/" + a1 + "=");

Scanner k4 = new Scanner(System.in);

int q4;

while (true) { try { q4 = k4.nextInt();

break;

}

catch (Exception e) { System.out.println("請輸入一個整數。");

}

}

if (q4 == sum4) { System.out.println("答案正确");

zq = zq + 1;

}

else { System.out.println("答案错误");

cw = cw + 1;

}

break;

}

int sum4 = a1 / a2;

System.out.print(a1 + "/" + a2 + "=");

Scanner k4 = new Scanner(System.in);

int q4;

while (true) { try { q4 = k4.nextInt();

break;

}

catch (Exception e) { System.out.println("請輸入一個整數。");

}

}

if (q4 == sum4) { System.out.println("答案正确");

zq = zq + 1;

}

else { System.out.println("答案错误");

cw = cw + 1;

}

break;

}

}

System.out.println("答题结束,一共" + s + "道题," + "正确" + zq + "个,错误" + cw + "个。");

}

public void hunHe() { Random random = new Random();

Scanner scanner = new Scanner(System.in);

int x = 0;

int y = 0;

int z = 0;

int answer;

int s = random.nextInt(4);

for (int i = 0; i < 4; 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)) { cw++; } else { zq++; } break;

case 1: if (y == 0) { System.out.println("请输入" + x + "+" + "(" + z + "/" + y + ")" + "的结果;");

answer = scanner.nextInt();

if (answer != x + (z / y)) { cw++;

}

else { zq++;

}

break;

}

System.out.println("请输入" + x + "+" + "(" + y + "/" + z + ")" + "的结果;");

answer = scanner.nextInt();

if (answer != x + (y / z)) { cw++;

}

else { zq++; } break;

case 2: if (x < (y * z)) { System.out.println("请输入" + "(" + y + "*" + z + ")" + "-" + x + "的结果;");

  answer = scanner.nextInt();

if (answer != (y * z) - x) { cw++; } else { zq++; } break;

}

System.out.println("请输入" + x + "-" + "(" + y + "*" + z + ")" + "的结果;");

answer = scanner.nextInt();

if (answer != x - (y * z)) { cw++; } else { zq++; } break;

case 3: if (y == 0) { System.out.println("请输入" + x + "-" + "(" + z + "/" + y + ")" + "的结果;");

answer = scanner.nextInt();

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

if (answer != x - (y / z)) { cw++;

}

else { zq++;

}

}

}

}

}

posted @ 2015-10-25 19:43  想你的那个人  阅读(105)  评论(0)    收藏  举报