Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass value from one method into main method?
    text
    copied!<p>I'm creating a math program that asks the user how many digits they'd like to use, then asks them what kind of math they want to do. It then asks the user 10 math questions based on their answers. It then decides if they're right or wrong and displays an appropriate message. It then calculates their percentage of correct answers and displays a message based on the percentage.</p> <p>The problem is that the program never stops after 10 questions and never calculates the percentage or displays the final message. I believe this is at least partially occurring because I'm not passing the new "answeredTyped" value from the "askQuestion" method. Can someone explain how I can easily pass this new value back to the main method so that it'll stop at 10? Or perhaps tell me what else is keeping this from working correctly?</p> <p>I've asked quite a few questions about this program already, so I appreciate everyone's patience with me learning Java and having very little knowledge of the language.</p> <p>Thank you for any help! (also I apologize for any bad formatting here! After changing about a million things, it got ugly)</p> <pre><code>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 &gt; 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 &lt; 10) { askQuestion(difficulty, null, selectedOperator, answeredTyped, operators, correctAnswers); answeredTyped++; if (answeredTyped&gt;= 10) { if (((float) correctAnswers / answeredTyped) &gt;= 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++; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload