需要帮助制作随机数学生成器



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

到目前为止,我不知所措

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("/");  

      }
  }

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

好吧,您需要添加的是:

  • 计数答案

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

    • 将问题生成和答案评估放在一个循环中,重复10次
    • 在您的交换机中(即,当您选择运营商时)也计算正确答案:

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

因此,您的问题的"伪代码"解决方案如下所示:

  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中的面向对象编程。这就是为什么我没有提供任何关于使用类有多好的提示。然而,如果你是,请告诉我,我会添加信息到我的帖子。

祝你好运!

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

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,还有其他选项可以处理意外的参数。

打印数字和操作,使用文件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);

    }
}

跟踪结果,并与用户输入进行比较,验证其是否正确

public static void main(String[]args)throws 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");
}

由于我不想向您提供这个问题的完整解决方案,而且您似乎对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.");
}

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

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();

相关内容

  • 没有找到相关文章

最新更新