-2

我应该做的是:编写一个程序,给用户 10 个随机数学问题,每次都要求答案,然后告诉用户他们是对还是错。每个问题应使用 2 个介于 1 和 20 之间的随机数和一个随机操作(+、-、* 或 /)。您将需要重新随机化每个数学问题的数字。您还应该跟踪他们解决了多少问题。最后,告诉用户他们做对了多少问题,并根据他们的结果给他们一条消息。例如,您可能会说“干得好”或“您需要更多练习”。</p>

到目前为止我不知所措

import java.util.Scanner; 

public class SS_Un5As4 {

 public static void main(String[] args){

 Scanner scan = new Scanner(System.in);

 int number1 = (int)(Math.random()* 20) + 1;

int number2 = (int)(Math.random()* 20) + 1;

 int operator = (int)(Math.random()*4) + 1;

  if (operator == 1)

  System.out.println("+"); 

 if (operator == 2) 

   System.out.println("-");

 if (operator == 3)

 System.out.println("*");

  if (operator == 4)
            System.out.println("/");  


      }
  }

我主要需要知道如何将这些随机数和运算符变成一个问题,以及如何对每个问题进行评分以查看它们是否错误。

4

5 回答 5

4

好吧,您需要添加的是:

  • 计算答案

    • 计算正确答案的变量(每次用户正确回答时递增);
    • 存储当前正确答案的变量;
    • 一个用于存储当前用户答案的​​变量(在下一个问题时刷新它,无需永远存储它,因为在您的情况下只需要统计信息);
    • 一个函数(让它被称为例如gradeTheStudent()),它使用几个条件来决定根据正确答案的数量打印出什么;
  • 制造问题

    • 将问题生成和答案评估置于一个循环中,重复 10 次;
    • 在您的开关中(即当您选择运算符时)还计算正确答案:

       switch(operator){
      
            case 1: {
            operation = "+";
            correctResult = number1 + number2;
            break;
         }
         case 2: ....
         case 3: ....
         case 4: ....
         default: break;
      }
      
    • 不要忘记检查用户是否输入了数字或其他内容(您可以使用异常或简单条件)。

因此,针对您的问题的“伪代码”解决方案如下所示:

  String[] reactions = ["Awesome!","Not bad!","Try again and you will get better!"]
  num1 = 0
  num2 = 0
  operator = NIL
  userScore = 0
  userAnswer = 0
  correctAnswer = 0

  def function main:

      counter = 0
      for counter in range 0 to 10:
          generateRandomNumbers()
          correctAnswer = generateOperatorAndCorrectAnswer()
          printQuestion()
          compareResult()

      gradeStudent()

  def function generateRandomNumbers:
      # note that you have already done it!

  def function generateOperatorAndCorrectAnswer:
      # here goes our switch!
      return(correctAnswer);

  def function printQuestion:
      print  "Next problem:" + "\n"
      print num1 + " " + operator + " " + num2 + " = " + "\n"

  def function compareResult(correctAnswer):
      # get user result - in your case with scanner
      if(result == correctAnswer) 
                print "Great job! Correct answer! \n"
                userScore++
      else print "Sorry, answer is wrong =( \n"

  def function gradeStudent (numOfCorrectAnswers):
      if(numOfCorrectAnswers >= 7) print reactions[0]
      else if(numOfCorrectAnswers < 7 and numOfCorrectAnswers >= 4) print reactions[1]
      else print reactions[2]

一般建议:不要试图一次性解决所有问题。一个好的方法是创建小函数,每个函数都执行其独特的任务。问题分解也是如此:你只需要写下你认为你需要什么来模拟情况,然后一步一步地去做。

注意:据我所知,从您当前的功能来看,您并不熟悉 Java 中的面向对象编程。这就是为什么我没有提供任何关于使用类的好处的提示。但是,如果你是,那么请告诉我,我会在我的帖子中添加信息。

祝你好运!

于 2013-06-28T06:35:51.450 回答
2

例如,您可以使用类似的东西:

public class Problem {
    private static final int DEFAULT_MIN_VALUE = 2;
    private static final int DEFAULT_MAX_VALUE = 20;

    private int number1;
    private int number2;
    private Operation operation;

    private Problem(){
    }

    public static Problem generateRandomProblem(){
        return generateRandomProblem(DEFAULT_MIN_VALUE, DEFAULT_MAX_VALUE);
    }

    public static Problem generateRandomProblem(int minValue, int maxValue){
        Problem prob = new Problem();
        Random randomGen = new Random();

        int number1 = randomGen.nextInt(maxValue + minValue) + minValue;
        int number2 = randomGen.nextInt(maxValue + minValue) + minValue;

        prob.setNumber1(number1);
        prob.setNumber2(number2);

        int operationCode = randomGen.nextInt(4);
        Operation operation = Operation.getOperationByCode(operationCode);
        prob.setOperation(operation);

        return prob;
    }

    public int getNumber1() {
        return number1;
    }

    public int getNumber2() {
        return number2;
    }

    public Operation getOperation() {
        return operation;
    }

    public void setNumber1(int number1) {
        this.number1 = number1;
    }

    public void setNumber2(int number2) {
        this.number2 = number2;
    }

    public void setOperation(Operation operation) {
        this.operation = operation;
    }
}

另一个用于持有操作的类:

public enum Operation {
    PLUS,
    MINUS,
    MULTIPLY,
    DIVIDE;

    public double operationResult(int n1, int n2) {
        switch (this) {
            case PLUS: {
                return (n1 + n2);
            }
            case MINUS: {
                return n1 - n2;
            }
            case MULTIPLY: {
                return n1 * n2;
            }
            case DIVIDE: {
                return n1 / n2;
            }
        }
        throw new IllegalArgumentException("Behavior for operation is not specified.");
    }

    public static Operation getOperationByCode(int code) {
        switch (code) {
            case 1:
                return PLUS;
            case 2:
                return MINUS;
            case 3:
                return MULTIPLY;
            case 4:
                return DIVIDE;
        }
        throw new IllegalArgumentException("Operation with this code not found.");
    }
}

但您不必抛出 IllegalArgumentException,还有另一种处理意外参数的选项。

于 2013-06-28T06:37:25.253 回答
0

打印出数字和操作,使用文件 IO 读取用户输入,并执行跟踪已回答问题的逻辑代码:

public class SS_Un5As4 {

    public static void main(String[] args){

        Scanner scan = new Scanner(System.in);
        int number1 = (int)(Math.random()* 20) + 1;
        int number2 = (int)(Math.random()* 20) + 1;
        int operator = (int)(Math.random()*4) + 1;
        String operation = null;
        if (operator == 1)
            operation="+";      
        if (operator == 2) 
                operation="-";  
        if (operator == 3)
            operation="*";  
        if (operator == 4)
            operation="/";    
        System.out.println("Question "+number1+operation+number2);


    }
}

跟踪结果并与用户输入进行比较并验证其正确或错误

公共静态 void main(String[] args) 抛出 IOException{

    int number1 = (int)(Math.random()* 20) + 1;
    int number2 = (int)(Math.random()* 20) + 1;
    int operator = (int)(Math.random()*4) + 1;
    String operation = null;
    int result=0;
    if (operator == 1){
        operation="+";
        result=number1+number2;
    }
    if (operator == 2) {
        operation="-";
        result=number1-number2;
    }
    if (operator == 3){
        operation="*";  
        result=number1*number2;
    }
    if (operator == 4){
        operation="/";
        result=number1/number2;
    }
    System.out.println("Question "+number1+operation+number2);
    String result1 = new BufferedReader(new InputStreamReader(System.in)).readLine();
    if(result==Integer.parseInt(result1))
        System.out.println("Right");
    else
        System.out.println("Wrong");
}
于 2013-06-28T06:22:05.310 回答
0

由于我不想为您提供此问题的完整解决方案,而且您似乎对 Java 语言有所了解,所以我将写下我将如何继续/更改您作为开始的内容。

首先,我会将结果存储在您的运算符 if 语句中。结果是一个整数。

if (operator == 1) {
   operation="+";
   result=number1+number2;
}

在此之后,我将打印数学问题并等待用户回答。

System.out.println("What is the answer to question: " +number1+operation+number2);
userResult = in.nextLine();      // Read one line from the console.
in.close(); // Not really necessary, but a good habit.

在这个阶段,您所要做的就是将结果与用户输入进行比较并打印一条消息。

if(Integer.parseInt(userResult) == result) {
  System.out.println("You are correct!");
} else {
  System.out.println("This was unfortunately not correct.");
}

该解决方案或多或少是伪代码,并且缺少一些错误处理(例如,如果用户在答案中输入测试),我也将其拆分为方法,而不是将其全部放在 main() 中。下一步是使其面向对象(看看demi的答案)。祝你最终完成你的计划好运。

于 2013-06-28T06:42:34.600 回答
0
In regard to generating random math operations with +, -, * & / with random numbers your can try the following;


import java.util.*;
public class RandomOperations{
   public static void main(String[] args){

       Random `mathPro` = new Random();
       //for the numbers in the game
       int a = mathPro.nextInt(50)+1;
       int b = mathPro.nextInt(50)+1;

       //for the all the math operation result

       int add = a+b;
       int sub = a-b;
       int mult = a*b;
       int div = a/b;
       //for the operators in the game

       int x = mathPro.nextInt(4);

       /*
         -so every random number between 1 and 4 will represent a math operator

         1 = +
         2 = -
         3 = x
         4 = /

      */

       if(x == 1){

          System.out.println("addition");
          System.out.println("");
          System.out.println(a);
          System.out.println(b);
          System.out.println(add);

       }else if(x == 2){

          System.out.println("subtraction");
          System.out.println("");
          System.out.println(a);
          System.out.println(b);
          System.out.println(sub);

       }else if(x == 3){

          System.out.println("multiplication");
          System.out.println("");
          System.out.println(a);
          System.out.println(b);
          System.out.println(mult);

       }else{

          System.out.println("division");
          System.out.println("");
          System.out.println(a);
          System.out.println(b);
          System.out.println(div);

       }
  //This os for the user to get his input then convert it to a numbers that the program can
  //understand
       Scanner userAnswer = new Scanner(System.in);
               System.out.println("Give it a try");
                 int n = `userAnswer.nextInt();
于 2017-05-06T17:35:56.653 回答