Let's say I have 4 variables:
- Porto = 1 - Benfica = 2 - Sporting = 3 - Braga = 4
I have this code that makes me a shuffle of numbers in an array called "arr".
Integer[] arr = {1,2,3,4};
Collections.shuffle(Arrays.asList(arr));
for (int i = 0; i < arr.length; i++) {
}
Collections.shuffle(Arrays.asList(arr));
When running the program let's imagine that I get the result: [1, 3, 2, 4];
Now that I have this result I have 4 labels (team1, team2, team3, team4);
I want to put in the label "equipo1" the 1 result of the array and put the name of the team corresponding to that number in this case PORTO.
Another example ..
I want to put in the label "team2" the 2 result of the array, which is 3, so I want to put in this label the team corresponds to number 3, in this case Sporting ..
Can you explain how can I do this?
To run the program you have the code here below:
public class J2 extends JFrame {
private JPanel contentPane;
JLabel equipa1 = new JLabel("");
JLabel equipa2 = new JLabel("");
JLabel equipa3 = new JLabel("");
JLabel equipa4 = new JLabel("");
public void func(){
int Porto = 1;
int Benfica = 2;
int Sporting = 3;
int SCVitoria = 4;
Integer[] arr = {1,2,3,4};
Collections.shuffle(Arrays.asList(arr));
for (int i = 0; i < arr.length; i++) {
}
Collections.shuffle(Arrays.asList(arr));
System.out.println(Arrays.toString(arr));
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
J2 frame = new J2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public J2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 553, 405);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblJornada = new JLabel("Jornada 1");
lblJornada.setBounds(61, 11, 80, 14);
contentPane.add(lblJornada);
JButton btnNewButton = new JButton("Gerar Jornada");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
func();
}
});
btnNewButton.setBounds(125, 139, 139, 23);
contentPane.add(btnNewButton);
equipa1.setHorizontalAlignment(SwingConstants.CENTER);
equipa1.setBounds(20, 36, 139, 14);
contentPane.add(equipa1);
equipa2.setHorizontalAlignment(SwingConstants.CENTER);
equipa2.setBounds(169, 36, 139, 14);
contentPane.add(equipa2);
equipa3.setHorizontalAlignment(SwingConstants.CENTER);
equipa3.setBounds(20, 86, 139, 14);
contentPane.add(equipa3);
equipa4.setHorizontalAlignment(SwingConstants.CENTER);
equipa4.setBounds(169, 86, 139, 14);
contentPane.add(equipa4);
}
}