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

时间:2013-06-28 06:07:45

标签: java netbeans

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

到目前为止,我处于亏损状态

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


      }
  }

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

5 个答案:

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

祝你好运!

答案 1 :(得分: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,还有另外一个处理意外参数的选项。

答案 2 :(得分: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);


    }
}

跟踪结果并与用户输入进行比较并验证其是对还是

public static 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");
}

答案 3 :(得分:0)

由于我不想给你一个完整的解决方案来解决这个问题,而你似乎对Java语言有一些了解,我将简要写下如何继续/改变你的开始。

首先,我将结果存储在运算符if语句中。结果是一个int。

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的答案)。祝你最终完成你的计划。

答案 4 :(得分: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();