Reputation: 75
I'm having a bit of trouble here. I'm making a math program that asks the user for the number of digits to use in the math questions. It then asks for the type of math problem, and asks the user 10 questions. It checks to see if the answer is right and displays an appropriate message. If wrong, the user can retry the question forever. It then calculates if they scored over 75% and displays a message.
All of the popups work just fine, but for whatever reason it keeps telling me that the answer is wrong no matter what I enter. I tried to make sure all of the variable types matched up (doubles) so that there wouldn't be any conversion issues with the response, but I'm new to Java so I have no idea if I did that correctly.
Is there any chance you guys can spot the issue with my code? Any help is greatly appreciated. Thanks for viewing.
Also thanks to those of you who have already helped me with other issues in this project, I'm still learning Java!
import java.util.*;
import javax.swing.JOptionPane;
/**
*
*/
/**
* @author Tyler
*
*/
public class Program {
/**
* @param args
*/
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 Program().askQuestion(difficulty, null, arithmeticMethod, arithmeticMethod, operators, arithmeticMethod); }
public static boolean checkResponse (double primaryInt, double secondaryInt, String operatorText, double response){
if (operatorText.equals("1")){
return (primaryInt + secondaryInt) == response;
}
else if (operatorText.equals("2")){
return (primaryInt - secondaryInt) == response;
}
else if (operatorText.equals("3")){
return (primaryInt * secondaryInt) == response;
}
else if (operatorText.equals("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;
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];
while(!correctAnswer && answeredTyped < 10) {
double response = Double.parseDouble (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.");
}
}
}
}
Upvotes: 0
Views: 3812
Reputation: 838
I ran your program through eclipse debugger and the problem is in the checkResponse method. The operatorText String is populated from your array {"plus", "minus", "times", "divided by"} but you are checking for operatorText.equals("1") etc. This means that none of your check response method is ever actually being run and the method will always just fall through to returning false which explains the behaviour you are experiencing. Change it to operatorText.equals("plus") and you should be good to go.
Tip: Using an IDE like Eclipse and setting up some breakpoints makes it really easy to troubleshoot this kind of issue.
Upvotes: 2
Reputation: 24454
You're doing integer
division which results in an integer
, not a float
or double
value. For example:
int a = 5;
int b = 2;
System.out.println(a / b); // will print 2
System.out.println((double)a / b); // will print 2.5
Upvotes: 3
Reputation: 3816
Have you tried throwing an exception when checkResponse fails to find an operator? I don't see why an invalid operater input should have the same result as getting a question wrong.
If you had that, I'd expect you'd see that none of the if statements in that function resolve to true...why are you comparing to "1", "2", "3", and "4", when that isn't how you defined your operator text?
Upvotes: 2
Reputation: 68
I put this code into Eclipse and it throws me 8 red flags and 5 yellow. Most of the red flags have to do with the methods 'pow' and 'random' being undefined for 'Math'. Could you be trying to call something that you haven't actually written?
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];
This could possibly be fixed by changing your class name to something other than a standard 'Math' if you are actually trying to call the java Math methods.
Upvotes: 0