# Thread: Problem with Math Problems Program

1. Member
Join Date
Apr 2015
Posts
7
Rep Power
0

## Problem with Math Problems Program

Hi, so I have an assignment to create a program that prompts the user to ask how many math questions they would like to answer, and to select a difficulty of 1 or 2. Then they are asked random questions using the various math symbols and the program will keep track of the score and at the end, will print the statistics. In my code you can see what should be printed out. I have written majority of the code but I have been running into a few problems and I was hoping someone might be able to help me. Here are the directions first:

Originally Posted by directions
Your program should have one class file containing three methods. You should name this class
MathPractice. The main method will be where most of your code will be, but you should
use separate methods for asking and validating the two questions asked at the beginning of each
set of questions.

First, use a method called getNumberOfQuestions to ask the user how many questions
they want. The user should be repeatedly asked this question until a positive integer is
entered. This value should be returned to the calling program.

Second, use a method called getQuestionDifficulty to ask the user their desired question
difficulty level. The user should be repeatedly asked this question until they enter a 1 or
2. This value should be returned to the calling program.

Level 1 questions should consist of integer arithmetic problems using the standard Java
operators: +, -, *, and /. The operands should be randomly generated integers in the range 0 to 9.
Although 0 is a valid operand, it should never be used as a divisor in problems involving
division.

Level 2 questions should consist of integer arithmetic problems using the operators used for level
1 and also the % operator. For level 2, the operands should be randomly generated integers in
the expanded range -9 to 9.

Once presented with a problem (e.g., “What is 9 - 5?”) users can enter an answer. After they
correct or wrong, and the count of correct and/or incorrect answers should be incremented.

Once the specified number of questions has been presented and answered, a message should be
displayed informing the user the number of questions they answered correctly and the percentage
correct. The user should then be asked if they would like another set of questions.
If they do, the process begins anew with the user being asked how many questions they want, etc. If they
do not, the user should be presented with statistics summarizing all of the question sets and
questions presented. These statistics should include the number of sessions, the total number of
questions, the total correct, the total incorrect, and the percentage correct.[/B]
Here is my code I have. One of the problems Im running into while creating the methods and returning is that in my main method, my "difficulty" and "questionnum" integers are saying that the variable has not been initialized, yet I have the return value and calling the other two methods which they are in, and have the user input the value. Another problem I was having earlier was I have my for loop inside my while loop and the for loop does not seem to run. Im new to java and to programming in general so any help would be great! Here is my code:

Java Code:
```import java.util.Random;
import java.util.Scanner;

public class MathPractice {

public static int getNumberOfQuestions(int questionnum) {

Scanner keyboard = new Scanner(System.in);
System.out.println("How many questions would you like?");
questionnum = keyboard.nextInt();
while (questionnum < 0)
{
}
return questionnum;

}

public static int getQuestionDifficulty(int difficulty) {

Scanner keyboard = new Scanner(System.in);
System.out.println("What difficulty would you prefer? (1 = low, 2 = high)");
difficulty = keyboard.nextInt();
while (difficulty < 1 || difficulty > 2)
{
System.out.println("Valid levels are 1 and 2. Please reenter.");
}
return difficulty;
}

public static void main(String[] args) {

int runagain = 0; //did the user ask to replay y/n?
int stats = 0;
int difficulty = getQuestionDifficulty(difficulty);
int questionnum = getNumberOfQuestions(questionnum);

Scanner keyboard = new Scanner(System.in);

while (runagain == 0)
{

for (int count = 0; count > questionnum; count++)
{

Random rand = new Random();
switch(rand.nextInt(4))
{
case 0:

int a = rand.nextInt(9);
int b = rand.nextInt(9);
System.out.println("What is " + a + "+" + b + "?");
{
System.out.println("Correct!");
}
count++;

case 1:

int c = rand.nextInt(9);
int d = rand.nextInt(9);
System.out.println("What is " + c + "-" + d + "?");
{
System.out.println("Correct!");
}
count++;

case 2:

int e = rand.nextInt(9);
int f = rand.nextInt(9);
System.out.println("What is " + e + "*" + f + "?");
{
System.out.println("Correct!");
}
count++;

case 3:

int g = rand.nextInt(9);
int h = rand.nextInt(9);
System.out.println("What is " + g + "/" + h + "?");
{
System.out.println("Correct!");
}
count++;

case 4:

int i = rand.nextInt(9);
int j = rand.nextInt(9);
System.out.println("What is " + i + "%" + j + "?");
{
System.out.println("Correct!");
}
count++;
default:
runagain = 0;
break;

}
}

System.out.println("You answered " + correctanswer + " out of " + questionnum + "correctly!");
stats = 1;

if (stats == 1)
{
System.out.println("Session Statistics");
System.out.println("");
System.out.println("------------------");
System.out.println("");
System.out.println("# of Questions :%4d " + questionnum);
System.out.println("Percent Correct :%4d " + (correctanswer*100)/questionnum);
System.out.println("");
System.out.println("Goodbye!");
}

}

}
}```
Last edited by JosAH; 04-17-2015 at 08:10 AM. Reason: changed the format of part of the post ...

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,717
Rep Power
17

## Re: Problem with Math Problems Program

my "difficulty" and "questionnum" integers are saying that the variable has not been initialized
The relevant line of code is

Java Code:
`int difficulty = getQuestionDifficulty(difficulty);`
The compiler is grumbling about the *second* use of the difficulty variable. The problem is that you are trying to pass the value of difficulty to the getQuestionDifficulty() method and yet difficulty does not have a value at this stage. As a general rule you cannot use a variable like this until it has been initialised.

The solution is simply not to use an argument with this method. After all you don't use the difficulty argument in any essential way in this method. True, you return its value, but before that you assign it a value making the value that you passed the method redundant.

So declare the method as

Java Code:
```public static int getQuestionDifficulty() {
// slightly modified code here
}```
and call it using

Java Code:
`int difficulty = getQuestionDifficulty();`
I have my for loop inside my while loop and the for loop does not seem to run.
You won't know what happens when the program is run until you have successfully compiled it.
Last edited by pbrockway2; 04-17-2015 at 06:47 AM.

3. Member
Join Date
Apr 2015
Posts
7
Rep Power
0

## Re: Problem with Math Problems Program

I have figured out why the two integers not initializing and have solved the problem. Now I am back to the issue of once my program gets to the "while runagain==0" and then I have the for loop inside the while loop, my program does nothing but terminate. This happens right after I answer my two beginning questions prompting the user how many questions to get and what the difficulty is. EDIT reading your response right now pbrockway2

pbrockway2: Yes, I was able to fix that first issue of the variables not initializing. I am back at the issue where my program terminates once it reaches the while loop portion. Any thoughts on that?
Last edited by Alex; 04-17-2015 at 06:55 AM.

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,717
Rep Power
17

## Re: Problem with Math Problems Program

Also at www.javaprogrammingforums.com. Please if you start a discussion at multiple places, post a link at each of them to all the others. That way everybody taking part knows what else is being said. Be aware that some people won't spend time considering a problem if they think it might have been dealt with elsewhere.

my program does nothing but terminate. This happens right after I answer my two beginning questions
So, what's the code now?

Notice that you can use System.out.println() to check that things have the values you think they have:

Java Code:
```while (runagain == 0)
{
System.out.println("About to start for loop. questionum=" + questionnum);
for (int count = 0; count > questionnum; count++)
{
// etc```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•