2013-09-28 48 views
2

我正在嘗試創建一個Java數學培訓程序。我有一個在Javascript中的工作版本,你們中的一些人已經幫助我將它轉換爲Java。它應該向用戶詢問一個難題(然後在問題中使用這個數字來表示每個數字)。然後它詢問用戶要做什麼類型的數學(加法,減法,乘法,除法,隨機)。然後詢問用戶10個問題。當用戶回答時,它會告訴他們他們是對還是錯。如果錯了,他們會繼續嘗試這個問題。在10個問題結束時,它會計算出您是否超過75%,並顯示適當的答案。完整說明:askQuestion()方法中的Java邏輯問題

enter image description here

我終於得到了大部分工作正常,才發現,數學本身就是錯誤的。 有時如果我輸入一個2的難度,它只會給出1位數(它基本上不計算難度)。此外,它總是告訴我,我的數學是錯誤的。你有沒有可能發現邏輯上有什麼問題?

感謝您的任何幫助。

import java.util.*; 
import javax.swing.JOptionPane; 

public class Assignment2 { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     Scanner input = new Scanner(System.in); 
     int difficulty = 1; 
     String[] operators = {"plus", "minus", "times", "divided by"}; 
     int selectedOperator = 1; 
     int correctAnswers = 0; 
     int answeredTyped = 0; 

     int difficultyInput = Integer.parseInt(
      JOptionPane.showInputDialog(
       "Please choose the difficulty. " + 
        "Enter the number of digits to use in each problem.")); 

     if (difficultyInput > 0) { 
      difficulty = difficultyInput; 
     } 
     int arithmeticMethod = Integer.parseInt(
      JOptionPane.showInputDialog(
       "Choose an arithmetic problem to study: " + 
        "1 = Addition Only, 2 = Subtraction Only, " + 
         "3 = Multiplication Only, 4 = Division Only, " + 
          "5 = Random Problems")); 

     selectedOperator = arithmeticMethod; 
     new Assignment2().askQuestion(
       difficulty, null, arithmeticMethod, 
       arithmeticMethod, operators, arithmeticMethod); 
    } 

    public static boolean checkResponse (
      int primaryInt, int secondaryInt, 
       String operatorText, float response){ 
     boolean result = false; 
     switch (operatorText) { 
      case "1": 
       return (primaryInt + secondaryInt) == response; 
      case "2": 
       return (primaryInt - secondaryInt) == response; 
      case "3": 
       return (primaryInt * secondaryInt) == response; 
      case "4": 
       return (primaryInt/secondaryInt) == response; 
     } 
     return false; 
    } 

    public static String displayResponse (boolean isCorrect) { 
     int randomIndex = (int) (Math.floor(Math.random() * (4 - 1 + 1)) + 1); 
     switch (randomIndex) { 
      case 1: 
       return isCorrect ? "Very Good!" : "No. Please try again."; 
      case 2: 
       return isCorrect ? "Excellent!" : "Wrong. Try once more."; 
      case 3: 
       return isCorrect ? "Nice Work!" : "Don\'t give up!"; 
      case 4: 
       return isCorrect ? "Keep up the good work!" : "No. Keep trying."; 
     } 
     return "Oops..."; 
    } 

    public static void askQuestion(
      int difficulty, String operatorText, 
      int selectedOperator, int answeredTyped, 
      String[] operators, int correctAnswers) { 
     boolean correctAnswer = false; 
     int primaryInt = (int) Math.floor(Math.pow(10, difficulty-1) + Math.random() * 9 * Math.pow(10, difficulty-1)); 
     int secondaryInt = (int) Math.floor(Math.pow(10, difficulty-1) + Math.random() * 9 * Math.pow(10, difficulty-1)); 
     operatorText = (selectedOperator == 5) ? operators[(int) Math.floor(Math.random() * operators.length)] : operators[selectedOperator - 1]; 

     while(!correctAnswer && answeredTyped < 10) { 
      float response = Float.parseFloat (JOptionPane.showInputDialog("How much is " + primaryInt + " " + operatorText + " " + secondaryInt + "?")); 
      correctAnswer = checkResponse (primaryInt, secondaryInt, operatorText, response); 
      JOptionPane.showMessageDialog(null, displayResponse(correctAnswer)); 

      answeredTyped++; 

      if(correctAnswer) 
      correctAnswers++; 
     } 
     { 
      while(answeredTyped < 10) { 
       askQuestion(0, null, 0, 0, null, 0); 
      } 
      if((correctAnswers/answeredTyped) >= 0.75) { 
       JOptionPane.showMessageDialog(
        null, "Congratulations, you are ready to " + 
        "go on to the next level!"); 
      } else { 
       JOptionPane.showMessageDialog(
        null, "Please ask your teacher for extra help."); 
      } 
     } 
    } 
} 

回答

2

在你的代碼,你在呼喚askQuestion你做

new Assignment2().askQuestion(arithmeticMethod, null, arithmeticMethod, arithmeticMethod, operators, arithmeticMethod); 
} 

但看你的方法定義,它應該是,要傳遞的,而不是難度

new Assignment2().askQuestion(difficulty, null, arithmeticMethod, arithmeticMethod, operators, arithmeticMethod); 
} 
+0

感謝arthmeticMethod很多Shivam!看起來像是讓這個變化解決了難題,但它仍然告訴我答案是錯誤的。對此有任何想法?我是一般的編程新手,所以任何幫助都非常感謝! – user2774647

+0

這是因爲你的檢查響應函數。你正在比較2個雙打,這將永遠不會準確,因此,將永遠評估爲假。經驗法則:如果你想對它進行條件化,就不要比較雙倍,引入不必要的錯誤。您可能需要評估您的答案以確定最接近的int或最多2個小數點,然後進行檢查。 –

+0

好的,我把雙打變成了整數,它似乎仍然給我一個錯誤的答案......也許我沒有正確地改變它? 我編輯了原始問題以包含我的更新代碼。任何其他想法? 再次感謝您的幫助! – user2774647