一道JAVA作业题 求教 求设计程序!

1个回答

  • 亲测可运行。

    import java.io.*;  public class Pratices {  public static void main(String[] args) {  new Pratices().list_Pratices();  } //产生随机数

    public int random_Num(int range) {  return (int) (Math.random() * range);  }  public void list_Pratices() {  int num_1, num_2, temp;  int type = random_Num(2);  int count = 1;  int score = 0;  int right = 0;  int wrongtimes = 0;  while (count <= 10) {  type = random_Num(2);  num_1 = random_Num(20); //可以修改这里来更改范围  num_2 = random_Num(30); //同上 现在是一个数20 一个数30以内的随机数  wrongtimes = 0;  if (type == 0) {  System.out.print("(" + count + ") " + num_1 + " + " + num_2  + " = ");  } else if(type == 1){  if ((num_1 < num_2)) {  temp = num_1;  num_1 = num_2;  num_2 = temp;  }  System.out.print("(" + count + ") " + num_1 + " - " + num_2  + " = ");  }  int answer = this.getAnswer(count);  boolean flag = check(num_1, num_2, type, answer, count);  if (flag) {  right++;  System.out.println("回答正确");  score += this.getScore(wrongtimes);  } else {  while (wrongtimes < 2) {  wrongtimes++;  System.out.println("回答错误 " + wrongtimes + " 次");  answer = this.getAnswer(count);  flag = check(num_1, num_2, type, answer, count);  if (flag) {  score += this.getScore(wrongtimes);  right++;  wrongtimes = 0;  break;  }  }  if (wrongtimes == 3)  System.out.println("回答错误 ");  else  System.out.println("回答正确");  }  count++;  }  System.out.println("回答正确 : " + right);  System.out.println("回答错误: " + (10 - right));  System.out.println("获得分数: " + score);  System.out.println(getDegree(score));  }  public boolean check(int num_1, int num_2, int type, int my_Answer,  int count) {  int answer = 0;  if (type == 1) {  answer = num_1 - num_2;  } else if (type == 0) {  answer = num_1 + num_2;  }  return my_Answer == answer;  }  public int getAnswer(int count) {  int my_Answer = 0;  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  try {  my_Answer = Integer.parseInt(br.readLine());  } catch (IOException e) {  e.printStackTrace();  } catch (NumberFormatException e) {  System.out.println("输入有误");  return 0;  } finally {  if (count >= 10 (br != null)) {  try {  br.close();  } catch (IOException e) {  e.printStackTrace();  }  br = null;  }  }  return my_Answer;  }  public int getScore(int wrongtimes) {  if (wrongtimes == 0) {  return 10;  } else if (wrongtimes == 1) {  return 7;  } else if (wrongtimes == 2) {  return 5;  } else  return 0;  }  public String getDegree(int score) {  if (score > 90)  return "SMART";  else if (score > 80)  return "GOOD";  else if (score > 70)  return "OK";  else if (score > 60)  return "PASS";  else  return "TRY AGAIN";  }  }