Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Logic Issue in askQuestion() method
    primarykey
    data
    text
    <p>I'm trying to create a Java math training program. I have a working version in Javascript and some of you guys have already helped me convert it over to Java. It's supposed to ask the user a difficulty (which then uses that amount of digits for each number in the questions). It then asks the user what type of math to do (addition, subtraction, multiplication, division, random). It then asks the user 10 questions. As the user answers, it tells them if they're right or wrong. If wrong, they get to continue attempting the question. At the end of 10 questions, it calculates if you got over 75% of them right and displays an appropriate response. Full instructions:</p> <p><img src="https://i.stack.imgur.com/3VH8U.png" alt="enter image description here"></p> <p>I finally got most of it working correctly, only to find out that the math itself is wrong. Sometimes if I enter a difficulty of 2, it only gives 1 digit (it basically doesn't calculate the difficulty right). Also, it always tells me my math is wrong. Is there any chance you guys can spot anything wrong with the logic?</p> <p>Thanks for any help.</p> <pre><code>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 &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, 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 &amp;&amp; answeredTyped &lt; 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 &lt; 10) { askQuestion(0, null, 0, 0, null, 0); } if((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."); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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