# Solved: Java programming



## BellRaskolnikov (Oct 26, 2012)

Im having a difficult time figuring out how to create a MathTutor program that displays math problems by randomly generating two numbers, 1 through 10 and an operator (*,+,-,/), and prompts the user for an answer. While the program should check the answer, display a message, and the correct answer if necessary. The program should keep track of how many questions the user has answered and how many of those where answered correctly. This is what i got so far:

import java.util.Scanner;
public class mathTutor{
public static void main(String [] args){
Scanner reader = new Scanner(System.in);
int a = (int)(Math.random * 11 + 1);
int b = (int)(Math.random * 11 + 1);
int operator = (int)(Math.random * 5 + 1);
int correct= 0;
while (program!=-1){
if (operator==1){
System.out.print("number " + a + " + " + " number " + b);
int answer = a + b;
System.out.print("Enter the answer ");
int guest = reader.nextInt();
guest = answer;
System.out.print("Correct! ");
correct++;}
if (operator==2){
System.out.print("number " + a + " - " + " number " + b);
answer = a-b;
System.out.print("Enter the answer ");
guest = reader.nextInt();
guest = answer;
System.out.print("Correct! ");
correct++;}
if (operator==3){
System.out.print("number " + a + " * " + " number " + b);
answer = a*b;
System.out.print("Enter the answer ");
guest = reader.nextInt();
guest = answer;
System.out.print("Correct! ");
correct++;}
if (operator==4){
System.out.print("number " + a + " / " + " number " + b);
answer = a/b;
System.out.print("Enter the answer ");
guest = reader.nextInt();
guest = answer;
System.out.print("Correct! ");
correct++;
else 
System.out.print("Wrong! Want to solve another problem? ");
program = reader.nextInt;}
}
}
}

There are a few missing pieces and this thread of error keep showing up: java:42: 'else' without 'if'. How can I rearrange the program to run the way i want? Thanks.


----------



## Ent (Apr 11, 2009)

Try using the same If structure (or look at the Switch command) to display the question and work out the value for the answer variable, then write the bit of code once at the end that records their answer and checks whether they're right. 
To use the else command you have to close the If Brackets and start new ones
if (answer == guest) {
// CODE IF CORRECT 
} else {
// CODE IF INCORRECT
}


As a general tip, subtraction and especially division can get really awkward if you just run it that way round. For example if it comes out with 3/7, that's really hard if not impossible for a human to answer (It's a recurring decimal). Subtraction is also awkward because you usually want to make sure the first number is larger than the second, but at least negative numbers can be typed in. 
A better way of doing the division problem is as follows:
Generate A and B randomly, each between the limits (1 to 10)
Work out what A * B is.
Print out (A * B) " / " B
The answer is A, which you already know is an integer.


----------



## BellRaskolnikov (Oct 26, 2012)

Finishing product at the end....
import java.util.Scanner;
public class mathTutor{
public static void main(String [] args){
Scanner reader = new Scanner(System.in);
int num1=0;
int num2=0;
int operator=0;
int answer=0;
int questionAnswered=0;
int answerCorrectly=0;
int total=0;
num1 = (int)(Math.random()*11 + 1);
num2 = (int)(Math.random()*11 + 1);
operator = (int)(Math.random()* 5 + 1);
if (operator==1){{
System.out.print("Number " + num1 + " + " + "number " + num2 + " equals: ");
answer = reader.nextInt();
questionAnswered++;
total = num1+num2;}
if (answer!=total){
System.out.print("Wrong answer, the correct answer is " + total); }
else {
System.out.print("Correct!");
answerCorrectly++;}}
if (operator==2){{
System.out.print("Number " + num1 + " - " + "number " + num2 + " equals: ");
answer = reader.nextInt();
questionAnswered++;
total = num1-num2;}
if (answer!=total){
System.out.print("Wrong answer, the correct answer is " + total); }
else{
System.out.print("Correct!");
answerCorrectly++;}}
if (operator==3){{
System.out.print("Number " + num1 + " * " + "number " + num2 + " equals: ");
answer = reader.nextInt();
questionAnswered++;
total = num1*num2;}
if (answer!=total){
System.out.print("Wrong answer, the correct answer is " + total); }
else{
System.out.print("Correct!");
answerCorrectly++;}}
if (operator==4){{
System.out.print("Number " + (num1*num2) + " / " + "number " + num2 + " equals: ");
answer = reader.nextInt();
questionAnswered++;
total = (num1*num2)/num2;}
if (answer!=total){
System.out.print("Wrong answer, the correct answer is " + total); }
else{
System.out.print("Correct!");
answerCorrectly++;}}
}
}

Thank you for the help


----------

