Page 1 of 4 123 ... LastLast
Results 1 to 20 of 62

Thread: Math game

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

    Default Math game

    Hello, I am wondering what would be the best way to go about doing a simple math game. I want to ask the user 10 numeracy questions for example,

    a * b = (user-input) - correct/wrong!
    y - z = (user-input) - correct/wrong!
    e + t = (user-input) - correct/wrong!

    and so forth.

    What I have been considering, is creating an array of integers which elements hold 20 different random numbers created by the Random util. I'm just not sure how to generate random operations, for example, I don't want to have say

    Java Code:
     System.out.println(num[rand1] + " * " + num[rand2] + " = " + input);
    because then the first question will always be multiplication. how would I be able to generate random operators? Do I need to create a char array holding the math operators? and would I be able to concatenate a char array with an int array?

    Thanks very much for any feedback.
    [A!B]Java

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You can use an array of char and a random index. What you concatenate are array elements not arrays. Also investigate the String and PrintStream printf() methods.

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

    Default

    For example,

    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    
    public static void sums() {
    
         char operators = new char[4];
    
         operators[1] = '+';
         operators[2] = '-';
         operators[3] = '*';
         operators[4] = '/';
    
         Random rand1 = new Random();
         int index1 = rand1.nextInt(4);
         
    
    }
    Does this look about right at the moment?
    [A!B]Java

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

    Default

    Quote Originally Posted by Nanomech View Post
    For example,

    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    
    public static void sums() {
    
         char operators = new char[4];
    
         operators[1] = '+';
         operators[2] = '-';
         operators[3] = '*';
         operators[4] = '/';
    
         Random rand1 = new Random();
         int index1 = rand1.nextInt(4);
         
    
    }
    Does this look about right at the moment?
    Nope, that code will throw an ArrayIndexOutOfBounds exception (hint: array indexes start at zero).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Ah yes, I compiled and ran it and it gave me that exception. How would I display the value of whatever element the random function generates? More importantly, something I just cannot get my head around, once 2 random numbers are generated and an operator, how would I check to see that the answer is correct? Because they will be random everytime, is there a way to determine the result of the sum?

    Thanks. I'm still looking at the PrintStream printf method.

    Regards,

    NM.
    [A!B]Java

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

    Default

    Quote Originally Posted by Nanomech View Post
    Ah yes, I compiled and ran it and it gave me that exception. How would I display the value of whatever element the random function generates? More importantly, something I just cannot get my head around, once 2 random numbers are generated and an operator, how would I check to see that the answer is correct? Because they will be random everytime, is there a way to determine the result of the sum?

    Thanks. I'm still looking at the PrintStream printf method.
    If you have two operands 'left' and 'right' and an operator (char) you can calculate the result with a simple switch-statement:

    Java Code:
    int left, right; // the two operands
    char op; // the operator
    int result;
    
    swich (op) {
       case '+': result= left+right; break;
       case '-': result= left-right; break;
    // etc. etc.
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    That is awesome, thank you very much for this code.

    Could I also ask do I need to create 20 Random object? Could this be done with a for loop?

    Also, I was advised to use the printf method. I've looked at it but I cannot figure out the code to insert into my class. I've imported java.io.*, i've seen the signature line you put in but what goes in the parenthesis?

    Regards,

    NM.
    [A!B]Java

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

    Default

    Quote Originally Posted by Nanomech View Post
    That is awesome, thank you very much for this code.

    Could I also ask do I need to create 20 Random object? Could this be done with a for loop?

    Also, I was advised to use the printf method. I've looked at it but I cannot figure out the code to insert into my class. I've imported java.io.*, i've seen the signature line you put in but what goes in the parenthesis?
    Yep, if you have to generate 20 random numbers you can easily use a for loop for the purpose. For that printf( ... ) method read the API documentation; it tells you all about its use. May I suggest you create a simple class Question that generates one equation with a pseudo random operator and a few handy methods, such as toString(), getAnswer() etc.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    So could it possibly look like this?

    Java Code:
    public void equation(char operator) {
    	 
    	     answer = (left + operator + right); 
    	 
    	 }
    [A!B]Java

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

    Default

    Quote Originally Posted by Nanomech View Post
    So could it possibly look like this?

    Java Code:
    public void equation(char operator) {
    	 
    	     answer = (left + operator + right); 
    	 
    	 }
    Have you tried a simple example? If you had, you could've noticed that this doesn't work at all. You are just guessing now ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    I was guessing yes because I am not quite sure how to create what you are telling me to. I've tried to read the official documentation on several occasions now, but just do not understand what it's saying to me.

    This is probably a bit out of my league tbh.

    Back to basics I go^^
    [A!B]Java

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I wouldn't worry too much about an object oriented solution (Question class etc) if that's new to you - although it is the way to go. You can do what you want with an array of char from which you select a random index. Getting the "answer" involves a switch on the index as described above.

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

    Default

    Java Code:
    	 		 //char array which elements hold the mathematical operators
    		 char[] operators = new char[5];
    		 
    		 operators[1] = '+';
    		 operators[2] = '-';
    		 operators[3] = '/';
    		 operators[4] = '*';
    		 //END OF CHAR ARRAY
    		 
    		 int[] chosenone = new int[21];
    		 
    	     
                 for (int x = 1;x<=20;x++) {
    	 
    	             Random rand = new Random();
    		         chosenone[x] = rand.nextInt(30);
    		         System.out.println(chosenone[x]);
    	         }
    Ok, so I have a char array holding the mathematical operators and also an integer array holding random numbers generated with the Random util and a for loop.

    Should my next step be to create the PrintStream printf method?
    [A!B]Java

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

    Default

    So is it the switchstatement that does all the equations? Would the switch statement have to fall inside the for loop?
    [A!B]Java

  15. #15
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    At the start of the thread you said you wanted to ask the user 10 simple questions. The way I would approach this would be to write a program that just asked a single question: in other words there would be no for loop.

    The program would choose a first random number, a second random number and a random char representing the operation. It would put these together and display the question and get the user's answer. It could also calculate the correct answer (using the switch statement) and give some feedback or whatever.

    Once this simple version was correct and working it would be time to think about the for loop and how the whole pose question, get answer, check answer cycle could be repeated 10 times. But first things first. I would start by posing a single question and checking the user's response.

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

    Default

    That's actually a really logical way to think about it and makes much sense!

    I am capable of doing the following:

    So when you say the program would choose 2 random numbers and 1 char, I'm guessing (just for now), that I would create the 2 random numbers using 2 different Random objects. If this is correct, would I need to create another Random object to get the index of whichever operator in a char array? Do i even need to create a char array? I know how to get data off a user and display the questions etc but it's the whole idea of which way to go about it.

    I'm really sorry but I want to understand everything I need to do. I do appreciate the feedback though.
    [A!B]Java

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

    Default

    Quote Originally Posted by Nanomech View Post
    So when you say the program would choose 2 random numbers and 1 char, I'm guessing (just for now), that I would create the 2 random numbers using 2 different Random objects. If this is correct, would I need to create another Random object to get the index of whichever operator in a char array? Do i even need to create a char array? I know how to get data off a user and display the questions etc but it's the whole idea of which way to go about it.

    I'm really sorry but I want to understand everything I need to do. I do appreciate the feedback though.
    A Random object is a (pseudo) random number generator; it is very well capable of generating more than one number:

    Java Code:
    String operators= "+-*/";
    Random r= new Random();
    int left= r.nextInt(100); // generate a number in the range [0, 100)
    int right= r.nextInt(100); // ditto
    char op= operators.charAt(r.nextInt(operators.length()));
    I didn't comment the last line; you have to figure out how it works.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Java Code:
    char op= operators.charAt(r.nextInt(operators.length()));
    Ok i'll have a go at this.

    Ok so you're searching the String operators using the charAt method. The charAt method searches the r.nextInt for a character position. Because r.nextInt returns a value between 0-100 it is set to the length of the String operators using operators.length() which is 3.

    It then stores a number in op between 0-3. Because the charAt method was used, it returns the value not the index position..

    Hope I am nearly there...

    edit: oh no, does it create another Random object which is set to the length of the String operators?
    ---------------------------------------------------------------------------------------------------------------------

    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.
    Last edited by Nanomech; 08-27-2011 at 01:58 PM.
    [A!B]Java

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

    Default

    Quote Originally Posted by Nanomech View Post
    Java Code:
    char op= operators.charAt(r.nextInt(operators.length()));
    Ok i'll have a go at this.

    Ok so you're searching the String operators using the charAt method. The charAt method searches the r.nextInt for a character position. Because r.nextInt returns a value between 0-100 it is set to the length of the String operators using operators.length() which is 3.

    It then stores a number in op between 0-3. Because the charAt method was used, it returns the value not the index position..

    Hope I am nearly there...
    Almost right; work from the inside out: the operators.length() method returns 4; so r.nextInt(4) returns a random number 0, 1, 2 or 3; the character at that position from the String operators is assigned to variable op. If the entire thing is evaluated, variable op contains one of the characters from the String operators.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    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?

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

    Thanks so much.
    [A!B]Java

Page 1 of 4 123 ... 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
  •