2013-09-29 115 views
0

我正在創建一個數學程序,詢問用戶他們想使用多少位數,然後詢問他們想要做什麼樣的數學運算。然後根據用戶的答案向用戶提出10個數學問題。然後決定他們是對還是錯,並顯示適當的信息。然後計算正確答案的百分比,並根據百分比顯示一條消息。如何將一個方法的值傳遞給主方法?

問題是程序在10個問題後永不停止,從不計算百分比或顯示最終消息。我相信這至少部分是因爲我沒有從「askQuestion」方法中傳遞新的「answersTyped」值。有人可以解釋我如何輕易地將這個新值返回到主要方法,以便它能夠在10點停止?或者也許告訴我還有什麼可以使這個工作不正常?

我已經問過關於這個程序的很多問題了,所以我很感謝大家對我學習Java並且對這門語言知之甚少的耐心。

謝謝你的幫助! (也是我的任何不良的格式在這裏道歉!改變一百萬的東西后,它得到醜陋)

import java.util.Scanner; 

import javax.swing.JOptionPane; 

public class Assignment2 { 
    public static int answeredTyped = 0; 
    public static int correctAnswers = 0; 
    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     int difficulty = 1; 
     String[] operators = { "plus", "minus", "times", "divided by" }; 
     int selectedOperator = 1; 



     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, selectedOperator, 
       answeredTyped, operators, correctAnswers); 

     while (answeredTyped < 10) { 
      askQuestion(difficulty, null, selectedOperator, answeredTyped, 
        operators, correctAnswers); 
      answeredTyped++; 


      if (answeredTyped>= 10) { 
       if (((float) 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."); 
       } 
     } 
    } 
    } 

    public static boolean checkResponse(double primaryInt, double secondaryInt, 
      String operatorText, double response) { 
     if (operatorText.equals("plus")) { 
      return (primaryInt + secondaryInt) == response; 
     } else if (operatorText.equals("minus")) { 
      return (primaryInt - secondaryInt) == response; 
     } else if (operatorText.equals("times")) { 
      return (primaryInt * secondaryInt) == response; 
     } else if (operatorText.equals("divided by")) { 
      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; 
     double primaryInt = Math.floor(Math.pow(10, difficulty - 1) 
       + Math.random() * 9 * Math.pow(10, difficulty - 1)); 
     double secondaryInt = 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]; 


      double response = Double.parseDouble(JOptionPane 
        .showInputDialog("How much is " + primaryInt + " " 
          + operatorText + " " + secondaryInt + "?")); 
      correctAnswer = checkResponse(primaryInt, secondaryInt, 
        operatorText, response); 
      JOptionPane.showMessageDialog(null, displayResponse(correctAnswer)); 
if (correctAnswer) 
       correctAnswers++; 

      } 

     } 
+0

與您的問題無關,您是否使用IDE?大多數IDE都內置格式化器。如果您使用Eclipse,請按'ctrl + shift + F'來格式化您的代碼。 – Mohayemin

+0

我相信它是ctrl + I用於eclipse,而alt + shift + f用於netbeans。 (雖然也許有一個ctrl + Shift + f的日食)。 – Rogue

+0

我通常使用Eclipse,但實際上我現在在NetBeans中。感謝提示!也感謝你繼續前進和格式化我:) – user2774647

回答

0

你有兩個while循環,所以它會調用askQuestion()十次,再有就是askQuestion自己而循環:

主()

while (answeredTyped < 10) { 

askQuestion()

while (!correctAnswer && answeredTyped < 10) { 

嘗試僅使用一個while循環,並將總體答案的評估放在askQuestion()方法外

+0

我試着刪除第二個while循環(你剛剛刪除那行),但它仍然做同樣的事情。通過「在askQuestion()方法之外對答案進行評估」,你的意思是什麼?代碼的哪個部分會超出askQuestion()方法,它到底會發生什麼?就在該方法的最後一個括號之後? – user2774647

+0

我的意思是你有一個運行askQuestion()十次的while循環,然後在askQuestion()中,它看起來像問了十次問題並在每次迭代中評估整體測試。所以在這一點上,你要運行一個10個問題的測試10次,這意味着你要評估10次測試總共100次。嘗試在整個測試後評估所有內容(例如,askQuestion()可以返回正確問題數的int值)) – Rogue

+0

我在OP中更改了我的代碼以反映我的新代碼。它現在在10點後停止,但它永遠不會給我最後的信息。使用新代碼,我只需讓main方法調用askQuestion()10次,然後askQuestion就一次提出一個問題。我相信這是一種不好的技術,但是我對這件事感到厭惡,因爲我只是想盡可能地讓它工作。有關使用我的新代碼顯示最終消息的任何提示? – user2774647

0

您必須在main方法的while循環中增加answersType,而不是在另一個方法中增加,因爲您沒有將變量聲明爲全局變量

+0

我在OP中更改了我的代碼以反映我的新代碼。它現在在10點後停止,但它永遠不會給我最後的信息。有小費嗎?我宣稱它是全球性的,希望它可以提供幫助 – user2774647