0

I'm trying to make a small game like thing for school. It's designed to help you learn your times tables. What I want is for the multiplier of the table to be random each time (5x8 then 5x3 then 5x9 etc).

I've got the generating of the numbers in control with an array as can be seen below

public static Integer[] generateNumbers()
{
    Integer[] arr = new Integer[12];

    for(int j = 0; j < arr.length; j++)
    {
        arr[j] = j+1;
    }
    Collections.shuffle(Arrays.asList(arr));
    System.out.println(Arrays.asList(arr));
    return arr;
}

How can I make it so that every time the user clicks a button, the next number in the array is selected, baring in mind that the button is declared in another class, and the ActionListener is also declared elsewhere?

Oh and the array is available class-wide as the function is declared like this:

public static Integer[] arr = generateNumbers();
7
  • I don't see a button... Commented Apr 1, 2014 at 12:02
  • @feuerball I didn't put the button declaration in the post. I can if you want.. Commented Apr 1, 2014 at 12:03
  • You don't need to see it, the action listener will be called when the button is clicked. Commented Apr 1, 2014 at 12:04
  • I don't even see an ActionListener... Commented Apr 1, 2014 at 12:07
  • @feuerball the actionlistener is in another class. The button responds. That's not my problem... Commented Apr 1, 2014 at 12:10

2 Answers 2

1

Thematic answer

public class UnicornFrame extends JFrame {

     private Integer[] poneyArr = MyClassThatGeneratesNumbers.generateNumbers();
     private int poneyCounter = 0;
     private JButton poneyButton;

     public void poneyInit() {
         System.out.println("Unicorns are poney magical friends!");
         poneyButton = new JButton("OMG! Ponies!");
         // Java 8 Lambdas! Yey!
         poneyButton.addActionListener(e -> { 
             if (poneyCounter >= poneyArr.length) {
                  poneyArray = MyClassThatGeneratesNumbers.generateNumbers();
                  poneyCounter = 0;      
             }         
             Integer selected = poneyArr[poneyCounter++];   
             System.out.println("OMG! I have selected " + selected);
         });

         // other stuff

         add(poneyButton, BorderLayout.CENTER);
     }
}
Sign up to request clarification or add additional context in comments.

Comments

0

The button int he separate class is not needed, the action listener will communicate with your array whenever its clicked, the easiest way I see is to put a public method int the array's class, that takes an index, which then increments the index, and returns the element stored at it.

Have fun coding, but these assignments are meant for you to scratch your head, try writing some code, and let us know if it breaks, rather than asking for general answers.

5 Comments

I'm being stupid here - what do you mean by an index?
No problem, nothing wrong with asking :) index is how you access an element in an array so this 'array[3]' here 3 is the index, and that would return the 4th element in the array because arrays start at index 0.
I thought that may have been the case. Thanks.
Ok I've been pondering over this for a while now. How do I take the index in the first place?
That depends on how you are implementing things, and when you find out, pass it as a parameter.. or which ever suits best for how you did things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.