Page 2 of 4 FirstFirst 1234 LastLast
Results 21 to 40 of 62

Thread: Math game

  1. #21
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Nanomech View Post
    I did edit my post and put this:

    ---------------------------------------------------------------------------------------------------------------------

    This is wrong I think, it searches the operators variable using the charAt method. It creates a new Random object and sets it's length between 0 and 3. This will return whatever value the random object chose. It then stores the value in op.

    Which i believe to be, in other words, what you said?
    Yep, that is correct.

    Awesome that's great, so is it now a case of displaying the question and getting the user input?

    Thanks so much.
    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.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  2. #22
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    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!
    [A!B]Java

  3. #23
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Nanomech View Post
    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!
    You can put all your code in the body of a loop but it probably ends up as one big, ugly, unmaintable loop:"

    Java Code:
    for ( ... ) {
       // all sorts of stuff that swirls from left to right, up and down ...
    }
    I'd stick all the code in a separate method and keep the loop as simple as possible:

    Java Code:
    for ( ... ) {
       askQuestion();
    }
    It doesn't only look cleaner but it is also easier to maintain.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #24
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    Ok, so i've done a little work on this and ive come up with this:

    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!");
    		 }
    		 
    		 
    		 
    	 }
    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.

    Thank you so far, it is now up and running!
    Last edited by Nanomech; 08-27-2011 at 04:35 PM.
    [A!B]Java

  5. #25
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Nanomech View Post
    Java Code:
     public static void game3(int tracker, int numberOfTries) {
             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;
    	     }
    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.

    Thank you so far, it is now up and running!
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #26
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    I've added this into my code and printed out 100 questions and none of those contained 0.

    Java Code:
    	if (right == 0 || left == 0) {
    		     
    	 left = r.nextInt(30);
                 right = r.nextInt(30); 
                 op = operators.charAt(r.nextInt(operators.length()));
    		 
    		 }
    Is this the way to go?
    Last edited by Nanomech; 08-27-2011 at 04:36 PM.
    [A!B]Java

  7. #27
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Nanomech View Post
    I've added this into my code and printed out 100 questions and none of those contained 0.

    Java Code:
    	if (right == 0 || left == 0) {
    		     
    	 left = r.nextInt(30);
                 right = r.nextInt(30); 
                 op = operators.charAt(r.nextInt(operators.length()));
    		 
    		 }
    Is this the way to go?
    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:

    Java Code:
    int left= r.nextInt(50);
    int right= r.nextInt(50);
    
    if (op == '/')
       while (right == 0)
          right= r.nextInt(50);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #28
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    Brilliant, thanks for the help so far.
    [A!B]Java

  9. #29
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Nanomech View Post
    Brilliant, thanks for the help so far.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #30
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    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

  11. #31
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Nanomech View Post
    Ok, this is what I have got to. Seems to work just fine now.

    Java Code:
             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)
    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.
    cenosillicaphobia: the fear for an empty beer glass

  12. #32
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    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

    Peace dude.
    [A!B]Java

  13. #33
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Nanomech View Post
    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

    Peace dude.
    Nah, a 'thumbs up' is just as fine; b.t.w. have you read my last remark in my previous reply? I'm afraid you have to solve one last little problem ....

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #34
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    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:

    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("");
    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.
    [A!B]Java

  15. #35
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Nanomech View Post
    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.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #36
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    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

  17. #37
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Nanomech View Post
    if (input == result) {

    return answer = true;
    }
    else {

    }
    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:

    Java Code:
    return input == result;
    The expression 'input == result' evaluates to true or false and you return that value from the method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #38
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

    Default

    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

  19. #39
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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:

    Java Code:
    return input == result;
    The expression 'input == result' evaluates to true or false and you return that value from the method.

    kind regards,

    Jos
    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

  20. #40
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    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

Page 2 of 4 FirstFirst 1234 LastLast

Similar Threads

  1. math and GUI
    By urbanleg in forum AWT / Swing
    Replies: 3
    Last Post: 08-06-2011, 04:05 PM
  2. Replies: 1
    Last Post: 06-02-2011, 09:29 AM
  3. Math average.
    By Willywonka in forum New To Java
    Replies: 3
    Last Post: 04-16-2011, 07:38 PM
  4. Math.cos()
    By ravi1 in forum New To Java
    Replies: 5
    Last Post: 03-27-2011, 02:52 PM
  5. Create Math.sin without math.sin
    By vudoo in forum New To Java
    Replies: 11
    Last Post: 12-07-2010, 06:23 AM

Posting Permissions

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