Results 21 to 40 of 62
Thread: Math game
- 08-27-2011, 02:16 PM #21
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Yep, that is correct.
You're welcome of course; note that you can't to left+op+right because op is a char and chars are numbers, so this expression would just add all three values together mathematically while you want a String; the remedy is simple though: ""+left+op+right; this forces the evaluator to add each operator to a String and the result will be a String representation of the expression.Awesome that's great, so is it now a case of displaying the question and getting the user input?
Thanks so much.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-27-2011, 02:34 PM #22
heh I was afraid that wouldn't work because that is exactly what I had planned to do

Once again, thank you for exaplaining this and helping me get to the next stage. I'm going to go and play about a little with this, get the functionality of one question being asked and answered with the switch statement returning the results of the given sum. I suppose, once you have the functionality with one question, would you just put all of that code within a for loop or does it require a little more tweaking than just shoving it in a for loop?
Can't wait to start putting this together!!
Salute to thee!.gif)
[A!B]Java
- 08-27-2011, 03:06 PM #23
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
You can put all your code in the body of a loop but it probably ends up as one big, ugly, unmaintable loop:"
I'd stick all the code in a separate method and keep the loop as simple as possible:Java Code:for ( ... ) { // all sorts of stuff that swirls from left to right, up and down ... }
It doesn't only look cleaner but it is also easier to maintain.Java Code:for ( ... ) { askQuestion(); }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-27-2011, 04:00 PM #24
Ok, so i've done a little work on this and ive come up with this:
It seems to be working fine. What I would like to do is put the askQuestion() method into it's own class and just reference it from my game3() method. Do you know how to access a method from a different class? Purely just to keep it out of my main class im writing in because its making it long.Java Code:public static void game3(int tracker, int numberOfTries) { System.out.println(""); System.out.println("In the last round it took you " + numberOfTries + " attempts."); System.out.println("This will count towards your overall score."); System.out.println(""); System.out.println("The next round is a series of sums."); System.out.println("The aim of this game is to provide me with the correct answer."); System.out.println("--------------------------------------------------------------------------------"); System.out.println("Round 3 Commencing..."); System.out.println(""); for (int y=1;y<=10;y++) { askQuestion(); } } public static void askQuestion() { String operators = "+-*/"; Random r = new Random(); int result = 0; int trace = 0; int left = r.nextInt(50); // generate a number in the range [0, 100) int right = r.nextInt(50); // ditto char op = operators.charAt(r.nextInt(operators.length())); System.out.println(""); System.out.print(""left+" "+op+" "+right+" = "); switch (op) { case '+': result = left+right; break; case '-': result = left-right; break; case '/': result = left/right; break; case '*': result = left*right; break; } Scanner s = new Scanner(System.in); int input = s.nextInt(); if (input == result) { System.out.print("Correct!"); trace++; } else { System.out.print("Wrong!"); } }
Thank you so far, it is now up and running!Last edited by Nanomech; 08-27-2011 at 04:35 PM.
[A!B]Java
- 08-27-2011, 04:09 PM #25
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Hold on, it isn't quite running yet; first of all, change the comment line (see above); it looks silly at best. Next, the value of variable 'right' can be zero; the random number generator can generate that number. What would happen if you divide by zero? Your program will throw an Exception; you have to do something about that special situation before you go on.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-27-2011, 04:24 PM #26
I've added this into my code and printed out 100 questions and none of those contained 0.
Is this the way to go?Java Code:if (right == 0 || left == 0) { left = r.nextInt(30); right = r.nextInt(30); op = operators.charAt(r.nextInt(operators.length())); }Last edited by Nanomech; 08-27-2011 at 04:36 PM.
[A!B]Java
- 08-27-2011, 05:02 PM #27
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Not really, the Random object doesn't have 'memory' i.e. if a number x was generated, the chance for generating another x is as large as if the previous number wasn't an x. You should check the value for 'right' in a loop if the operator is a division, and keep generating an other number until it isn't zero:
kind regards,Java Code:int left= r.nextInt(50); int right= r.nextInt(50); if (op == '/') while (right == 0) right= r.nextInt(50);
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-27-2011, 06:10 PM #28
Brilliant, thanks for the help so far.

[A!B]Java
- 08-27-2011, 07:21 PM #29
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
So now you are able to generate a left and right value, as well as the operator op. You are able to find the value of left op right and you are able to represent the entire thing in String form. I conclude that you must be able to construct a proper Question class out of it all.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-27-2011, 07:51 PM #30
Ok, this is what I have got to. Seems to work just fine now.
Java Code:public static void game3(int tracker, int numberOfTries, int trace) { System.out.println(""); System.out.println("In the last round it took you " + numberOfTries + " attempts."); System.out.println("This will count towards your overall score."); System.out.println(""); System.out.println("The next round is a series of sums."); System.out.println("The aim of this game is to provide me with the correct answer."); System.out.println("--------------------------------------------------------------------------------"); System.out.println("Round 3 Commencing..."); System.out.println(""); for (int y=1;y<=10;y++) { askQuestion(); } System.out.println("you scored " + trace + "/10."); } public static void askQuestion() { String operators = "+-*/"; Random r = new Random(); int result = 0; int trace = 0; int left = r.nextInt(30); // generate a number in the range [1, 30) int right = r.nextInt(30); // generate a number in the range [1, 30) char op = operators.charAt(r.nextInt(operators.length())); if (op == '/') { while (right == 0) { right= r.nextInt(30); } } System.out.println(""); System.out.print(""+left+" "+op+" "+right+" = "); switch (op) { case '+': result = left + right; break; case '-': result = left - right; break; case '/': result = left / right; break; case '*': result = left * right; break; } Scanner s = new Scanner(System.in); int input = s.nextInt(); if (input == result) { System.out.print("Correct!"); trace++; } else { System.out.print("Wrong!"); } }[A!B]Java
- 08-27-2011, 07:56 PM #31
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Well done, except for those comment lines again: they're lying; you are generating a number in the range [0,30) for left and right, not the range [1,30) No comment is better than incorrect comment.
kind regards,
Jos
p.s. edit: does that 'trace' thing work and correctly tracks the number of correct answers?Last edited by JosAH; 08-27-2011 at 08:00 PM.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-27-2011, 07:59 PM #32
Well I couldn't have done it without your help so thank you once again. How many thanks is that?

Maybe I shoulda 'liked' your posts.gif)
Peace dude.[A!B]Java
- 08-27-2011, 08:09 PM #33
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
- 08-27-2011, 08:37 PM #34
Aha it doesn't tbh, I was midway through playing about with it when I replied but I know why also, because the method is in a for loop it is executing at the very beginning of the method everytime thus resetting the trace variable back to 0. I need to sit and think how to make it work. I managed to do a similar thing in another method in my class and got it working correctly, but if I'm not mistaken it's because if statement was not within the for loop statement. Here it is actually, i've just had a look:
I also used a boolean value, which was reset to false every loop and only became true if the user input was found, if so it prints correct and tracks it.Java Code:public class MyProgram { boolean decide; String input; int tracker = 0; public static void main(String[] args) { boolean decide; String input; int tracker = 0; String[] questions = new String[11]; questions[1] = "What is the largest continent?"; questions[2] = "Which animal has black and white stripes?"; questions[3] = "What is a skeleton made of?"; questions[4] = "Where do bats live?"; questions[5] = "Where does a king and queen live?"; questions[6] = "What colour does a rainbow start with?"; questions[7] = "What sport does Wayne Rooney play?"; questions[8] = "What animal goes 'oink oink'?"; questions[9] = "Which planet is the nearest to Earth?"; questions[10] = "What colour is a banana?"; String[] PosAns = new String[49]; PosAns[1] = "asia"; PosAns[2] = "Asia"; PosAns[3] = "ASIA"; PosAns[4] = "zebra"; PosAns[5] = "Zebra"; PosAns[6] = "zebras"; PosAns[7] = "Zebras"; PosAns[8] = "ZEBRAS"; PosAns[9] = "ZEBRA"; PosAns[10] = "BONE"; PosAns[11] = "BONES"; PosAns[12] = "bone"; PosAns[13] = "bones"; PosAns[14] = "Bone"; PosAns[15] = "Bones"; PosAns[16] = "cave"; PosAns[17] = "caves"; PosAns[18] = "Cave"; PosAns[19] = "Caves"; PosAns[20] = "CAVES"; PosAns[21] = "CAVE"; PosAns[22] = "castle"; PosAns[23] = "castles"; PosAns[24] = "Castle"; PosAns[25] = "Castles"; PosAns[26] = "CASTLE"; PosAns[27] = "CASTLES"; PosAns[28] = "red"; PosAns[29] = "Red"; PosAns[30] = "RED"; PosAns[31] = "FOOTBALL"; PosAns[32] = "Football"; PosAns[33] = "football"; PosAns[34] = "SOCCER"; PosAns[35] = "Soccer"; PosAns[36] = "soccer"; PosAns[37] = "pig"; PosAns[38] = "PIG"; PosAns[39] = "Pig"; PosAns[40] = "PIGS"; PosAns[41] = "Pigs"; PosAns[42] = "pigs"; PosAns[43] = "moon"; PosAns[44] = "Moon"; PosAns[45] = "MOON"; PosAns[46] = "YELLOW"; PosAns[47] = "yellow"; PosAns[48] = "Yellow"; System.out.println("Welcome to my custom made, fun and testing game."); System.out.println("Are you brainy enough to tackle the questions?!?"); System.out.println(""); System.out.println("The first game is a simple question and answer excercise."); System.out.println("You will be asked various questions which you will be asked to answer."); System.out.println("--------------------------------------------------------------------------------"); System.out.println("Round 1 Commencing..."); System.out.println(""); for (int dx=1;dx<=questions.length-1;dx++) { decide = false; System.out.println(questions[dx]); Scanner scan = new Scanner(System.in); input = scan.nextLine(); for (int ab=1;ab<PosAns.length-1;ab++) { if(PosAns[ab].equals(input)) { decide = true; } } if (decide == true) { System.out.println("Correct!"); System.out.println(""); tracker++; } else { System.out.println("Wrong!"); System.out.println(""); } } System.out.println(""); System.out.println("You scored " + tracker + "/10"); System.out.println("");[A!B]Java
- 08-27-2011, 08:48 PM #35
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
You could make your askQuestion() method return a boolean value: true if the question was answered correctly and false if an incorrect answer was given. The game3() method could update its own trace value according to the return value of the askQuestion() method. The latter method can forget all about its own (local) trace variable because it isn't needed anymore.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-27-2011, 09:20 PM #36
I've changed my code, just wondering if this is the way to return a value to another method. I've changed the method from 'void' to 'boolean', created a boolean variable answer and if the answer was correct, returned the answer variable as true.
Java Code:public static boolean askQuestion() { String operators = "+-*/"; Random r = new Random(); boolean answer = false; int result = 0; int left = r.nextInt(30); // generate a number in the range [1, 30) int right = r.nextInt(30); // generate a number in the range [1, 30) char op = operators.charAt(r.nextInt(operators.length())); if (op == '/') { while (right == 0) { right= r.nextInt(30); } } System.out.println(""); System.out.print(""+left+" "+op+" "+right+" = "); switch (op) { case '+': result = left + right; break; case '-': result = left - right; break; case '/': result = left / right; break; case '*': result = left * right; break; } Scanner s = new Scanner(System.in); int input = s.nextInt(); if (input == result) { return answer = true; } else { } }[A!B]Java
- 08-28-2011, 08:33 AM #37
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Else what? You're supposed to return false there because the input doesn't equal the result. It can be done in a much shorter way; have a look:
The expression 'input == result' evaluates to true or false and you return that value from the method.Java Code:return input == result;
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-28-2011, 10:41 AM #38
Member
- Join Date
- Aug 2011
- Posts
- 31
- Rep Power
- 0
it might be good to create a loop, for the array. increasing the array value by 1 each time, and assigning a random value to each array. ask the question, by using if statements to determine what type of question, ie. multiply, add or minus. then use a scanner method for the user input :) good luck
- 08-29-2011, 12:58 AM #39
Thanks for the info. When that line of code is reached and you return the value, does it automatically get returned to game3()? I'm sure i have to change the method type from void to boolean right? Once I do that, at compile i get an error message saying that i cannot be referenced from a non static method or something very similar.... Any ideas? I have tried google on so many occasions for just a simple explanation/example on how to return a variable, but all are just much too complicated for me to understand and go off-point.
Thank dude.Last edited by Nanomech; 08-29-2011 at 02:30 AM.
[A!B]Java
- 08-29-2011, 02:52 AM #40
Ok, so i've just read up some about returning variables from methods. So the type of variable you are returning must match the method declaration return type. Once the return statement is reached, it breaks out of the flow and returns back to the previous method and starts where it left off. What I don't understand is how to use the returned variable. For a simple example, I tried to print the value of a boolean out to the screen but I'm getting errors at compile.
Can anyone please explain how to return a variable to a different method and use it there?
Thanks very much,[A!B]Java
Similar Threads
-
math and GUI
By urbanleg in forum AWT / SwingReplies: 3Last Post: 08-06-2011, 04:05 PM -
Complete Game Engine for beginner and intermediate game programmers
By rdjava in forum Java GamingReplies: 1Last Post: 06-02-2011, 09:29 AM -
Math average.
By Willywonka in forum New To JavaReplies: 3Last Post: 04-16-2011, 07:38 PM -
Math.cos()
By ravi1 in forum New To JavaReplies: 5Last Post: 03-27-2011, 02:52 PM -
Create Math.sin without math.sin
By vudoo in forum New To JavaReplies: 11Last Post: 12-07-2010, 06:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks