如何在按钮点击时增加变量?

时间:2022-08-26 21:31:27

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).

我正在尝试为学校制作一个小游戏。它旨在帮助您了解您的时间表。我想要的是每次表的乘数是随机的(5x8然后是5x3然后是5x9等)。

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?

我怎样才能使每次用户单击按钮时,选择数组中的下一个数字,记住按钮是在另一个类中声明的,ActionListener也在其他地方声明?

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

哦,数组在类范围内可用,因为函数声明如下:

public static Integer[] arr = generateNumbers();

2 个解决方案

#1


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.

有趣的编码,但这些任务是为了让你挠头,尝试编写一些代码,让我们知道它是否中断,而不是要求一般的答案。

#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);
     }
}

#1


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.

有趣的编码,但这些任务是为了让你挠头,尝试编写一些代码,让我们知道它是否中断,而不是要求一般的答案。

#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);
     }
}