2013-06-28 57 views
-2

我應該做的是這樣的:編寫一個程序,給用戶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("/"); 


     } 
    } 

我大多需要知道如何將這些隨機數字和運算成一個問題,以及如何對每個問題進行評分,看看他們是否錯了。

+1

你在這方面有什麼嘗試?我會說你做了最難的部分.. –

+0

似乎有一些我失蹤,但我只是不知道該怎麼做。如果你能詳細說明這將是非常棒的。 – user2530677

+0

打印出來 =?閱讀用戶輸入。檢查答案並輸出消息。你可能想嘗試分解成函數。您還可以將操作符存儲在'String operators =「+ - * /」'中。你可以從那裏查找和打印.. –

回答

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


    } 
} 

保持結果的一首曲目,用戶比較輸入驗證其對錯

公共靜態無效的主要(字串[] 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"); 
} 
+0

非常感謝,我非常瞭解如何做到這一點,我可以得到所有的問題彈出和人可以回答他們,但我仍然對如何記錄無論他們是否正確。 – user2530677

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中的面向對象編程。這就是爲什麼我沒有提供關於使用類的好處的原因。然而,如果你是,那麼請讓我知道,我會添加信息到我的文章。

祝你好運!

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,也有處理意外論點的另一個選擇。

+0

完美的答案,以防OP真正理解OOP。 +1 – petajamaja

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的答案)。祝你好運,最終確定你的節目。

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