• Post Reply Bookmark Topic Watch Topic
  • New Topic

Drawing a Blank  RSS feed

 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create a simple java math question quiz using random operators, but keep sinking myself into deeper despair. The numbers must range from 0-9 and given an operator: +,-,/,*,%. Can't create a new method or use Case statements. Once the number of questions is run up, they are given the opportunity to have more questions. The code isn't finished but don't want to make it any worse. Please help! And ask Questions.

package marco;
import java.util.Scanner;
import java.util.Random;
public class Project {

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
System.out.println("How many questions do you want?");
int numberofQuestions = keyboard.nextInt();
int randomOne = rand.nextInt(10) ;
int randomTwo = rand.nextInt(10) ;
int correct = 0;
int count = 0;
int userAnswer = 0;
int solution = 0;
int questionNumber = 1;
int operatorSign = rand.nextInt(5);
String operator;
if (operatorSign == 0) {
operator = "+";

}
else if (operatorSign == 1) {
operator = "-";
}
else if (operatorSign == 2) {
operator = "*";
}
else if (operatorSign == 3) {
operator = "/";
}
else if (operatorSign == 4) {
operator = "%";
}

while (count < numberofQuestions) {
randomOne = rand.nextInt(9) + 1;
randomTwo = rand.nextInt(9) + 1;
System.out.println("Question #" + questionNumber + ": " + "What is " + (randomOne) + operator + (randomTwo));
userAnswer = keyboard.nextInt();
count++;
questionNumber++;

if (userAnswer == solution) {
System.out.println("Correct!");
correct++;
}
else {
System.out.println("Wrong... The answer is " + solution);
}
System.out.println("Would you like some more questions (y/n)");
String s1 = keyboard.next();
while ((s1.equalsIgnoreCase("y")) || (s1.equalsIgnoreCase("n"))) {
if (s1.equalsIgnoreCase("y")) {
System.out.println("How many questions do you want?");
numberofQuestions = keyboard.nextInt();

}
else {
System.out.println("Goodbye");
}


}
}
}

}
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got the operator part finally working!


 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
That block of ifs looks as if it should be replaced by a switch. I suggest you put the signs in an array.
You have far too much code all in one place. That should be refactored into multiple methods. you should not have so much code in your main method.
Lines 6 and 7 contain initialisations which are never used.

I added code tags (always use them) to your post. See how much better it looks I also added some new lines to the longest of your long lines, so you can see how to do it. I think they are still too long.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marco cucinotta wrote: . . . Can't create a new method or use Case statements. . . .
Why on earth not? That is a bit like being told to drive 50 miles without using 3rd gear.
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My instructions were to include just one method, the main method. Also, I haven't been taught switches yet, so I won't be able to use them. I know it looks bunched up but this is how it has to look. The problem is, the solutions are still coming back as integers, not decimals.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you are using integer arithmetic. I didn't see any double arithmetic in use.
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I changed them to floating points but even if I print out the solution as a float, the division problem still comes back as whole numbers.
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*Finally got the decimals to appear, but with the "printf" the next line that shows in the program is backed into the printf line. Is there a way to send the upcoming statement onto the next line with printf?************ "Wrong... The answer is 1.50Question #6: What is 1/7"


public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
System.out.println("How many questions do you want?");
int numberofQuestions = keyboard.nextInt();
int randomOne = rand.nextInt(9) + 1;
int randomTwo = rand.nextInt(9) + 1;
int correct = 0;
int count = 0;
float userAnswer = 0;
float solution = 0;
int questionNumber = 1;

while (count < numberofQuestions) {
randomOne = rand.nextInt(9) + 1;
randomTwo = rand.nextInt(9) + 1;
int operatorSign = rand.nextInt(5);
if (operatorSign == 0) {
System.out.println("Question #" + questionNumber + ": " + "What is " + (randomOne) + "+" +
(randomTwo));
solution = randomOne + randomTwo;
}
else if (operatorSign == 1) {
System.out.println("Question #" + questionNumber + ": " + "What is " + (randomOne) + "-" +
(randomTwo));
solution = randomOne - randomTwo;
}
else if (operatorSign == 2) {
System.out.println("Question #" + questionNumber + ": " + "What is " + (randomOne) + "*" +
(randomTwo));
solution = randomOne * randomTwo;
}
else if (operatorSign == 3) {
System.out.println("Question #" + questionNumber + ": " + "What is " + (randomOne) + "/" +
(randomTwo));
solution = (float) randomOne / randomTwo;


}
else if (operatorSign == 4) {
System.out.println("Question #" + questionNumber + ": " + "What is " + (randomOne) + "%" +
(randomTwo));

solution = randomOne % randomTwo;
}

userAnswer = keyboard.nextFloat();
count++;
questionNumber++;
if (userAnswer == solution) {
System.out.println("Correct!");
correct++;
}
else {
if (solution == (float) randomOne / randomTwo) {
System.out.printf("Wrong... The answer is " + "%.2f", solution);
}
else {
System.out.printf("Wrong...The answer is " + "%.0f", solution);

}
}
}
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what is the correct answer?

Don't use floating point arithmetic if you want precision. You can get the user to enter 0.14 0.143 0.1429 0.14286 0.142857 0.1428571 0.14285714 0.142857143 0.1428571429 0.14285714286 0.142857142857 0.1428571428571 0.14285714285714 0.142857142857143 and 0.1428571428571429 and have that program show all of the wrong for 1 ÷ 7.
At least use doubles. There is no need to cast, except possibly for the division because yo uare using integer arithmetic for everything else.
You use the %n tag in printf to make it go to the next line.
You can read about printf and similar in the Java Tutorials in at least two places: 1 2 and in the Formatter documentation (not easy to read).
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the answer does come out correctly, this is my problem. After the answer is given if the user enters a wrong number, the next question is on the same line. But now I can use multiple methods but I'm not sure what to break up.
"Question #4: What is 5*9
6
Wrong...The answer is 45Question #5: What is 9+9"
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marco cucinotta wrote:*Finally got the decimals to appear, but with the "printf" the next line that shows in the program is backed into the printf line. Is there a way to send the upcoming statement onto the next line with printf?

Yes. Use "%n". That tells printf() to print a "newline".

However - advice for next time: Don't write so much code BEFORE testing it. Thoroughly.

If your code needs five questions ... or fifty ... start with ONE (or possibly two) and get THAT working before you start adding reams and reams of stuff.

It's a really good habit to get into: Deal with ONE thing at a time.

HIH

Winston
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like this? When I run it and the answer is wrong an error appears and ends the program. But if I get rid of the "%.0f" it works, but I don't want a .0 to appear after an integer.


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marco cucinotta wrote:Like this?

No. The "%n" goes in the format String; not in the arguments.

See if you can work it out for yourself. If you run into problems, come back.

And just FYI: the format String shouldn't contain any '+'s; it's simply a literal. After all, isn't:

System.out.printf("Wrong... The answer is " + "%.2f", solution... )

the same as:

System.out.printf("Wrong... The answer is %.2f", solution... )
?

Winston
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works! Thanks that helped me out. The only problem I am getting now is with my division. When a division problem comes up, if I don't put a 0 in the front its wrong, but even when the answer is the same its wrong?
"Question #7: What is 1/9?
0.11
Wrong... The answer is 0.11"
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marco cucinotta wrote:It works! Thanks that helped me out. The only problem I am getting now is with my division. When a division problem comes up, if I don't put a 0 in the front its wrong, but even when the answer is the same its wrong?

Ah. That's because 0.11 is NOT the same as 1/9.

Think about what 1/9 actually is.

Now (quite a bit harder): Think about what you might need to do to allow 0.11 as an answer.

Winston
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1/9 is just a fraction? Does declaring certain variables as float and not double make a difference?
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1/9 is .1111111. I was only outputting the wrong answer as 2 decimal places, not dividing to get 2 decimal places. That is the problem. But how do I fix that?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could consider working out whether the answer is nearly correct.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marco cucinotta wrote:1/9 is .1111111. I was only outputting the wrong answer as 2 decimal places, not dividing to get 2 decimal places. That is the problem. But how do I fix that?

Right, so we've established that 1/9 is NOT equal to 0.11.

So, what you want is to work out whether what they typed in is "close enough", and that's a LOT trickier, given the vagaries of floating-point values (a very good page to bookmark, by the way).

One solution is to use BigDecimal instead of floating-point types, but the API of that class is quite involved.

Another might be to convert your actual result to a String that is correct to 2 decimal places (assuming that's your criteria for "close enough") and check if it matches the string typed in by the user. But what if they enter "0.111" instead?

Yet another might be to work out if the difference between the "real answer" and the answer entered by your user is less than some value.
What do you think that might be?

As you can see, there's quite a bit to think about...

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!