2013-09-29 81 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