Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- import java.util.Random;
- public class creaSquadre {
- private static final int MAX_GIOCATORI = 10;
- private static final int MAX_PER_SQUADRA = 5;
- private static String[] SQUADRA1 = new String[MAX_PER_SQUADRA];
- private static String[] SQUADRA2 = new String[MAX_PER_SQUADRA];
- private static int j = 0, k = 0;
- // private static String[] SQUADRA_1 = new String[MAX_PER_SQUADRA];
- // private static String[] SQUADRA_2 = new String[MAX_PER_SQUADRA];
- private static void inserisciGiocatori(String[] giocatori) {
- for (int i = 0; i < MAX_GIOCATORI; i++) {
- giocatori[i] = JOptionPane.showInputDialog("inserisci il "
- + (i + 1) + "° giocatore:");
- }
- }
- private static void stampaGiocatori(String[] giocatori) {
- for (int i = 0; i < MAX_GIOCATORI; i++)
- System.out.print(giocatori[i] + " _ ");
- System.out.println(" ");
- }
- private static boolean isPieno(String[] squadra) {
- boolean pieno = false;
- for (int i = 0; i < MAX_PER_SQUADRA; i++) {
- if (squadra[i] == null)
- break;
- else
- pieno = true;
- }
- return pieno;
- }
- private static void creaSquadre(String[] giocatori) {
- Random r = new Random();
- for (int i = 0; i < MAX_GIOCATORI; i++) {
- int a = r.nextInt(2);
- switch (a){
- case 0:{
- if (giocatori[i]!=SQUADRA1[j]){
- SQUADRA1[j]=giocatori[i];
- j++;
- } else if(giocatori[i]== SQUADRA1[j]){
- SQUADRA2[k]=giocatori[i];
- k++;
- } else if(giocatori[i]==SQUADRA1[j] && giocatori[i]==SQUADRA2[k]){
- creaSquadre(giocatori);
- }
- }
- case 1: {
- if (giocatori[i]!=SQUADRA2[k]){
- SQUADRA2[k]=giocatori[i];
- k++;
- } else if(giocatori[i]== SQUADRA2[k]){
- SQUADRA1[j]=giocatori[i];
- j++;
- } else if(giocatori[i]==SQUADRA1[j] && giocatori[i]==SQUADRA2[k]){
- creaSquadre(giocatori);
- }
- }
- }
- }
- System.out.println("squadra 1: ");
- for (int i = 0; i < MAX_PER_SQUADRA; i++) {
- System.out.println(SQUADRA1[i]);
- }
- System.out.println("squadra 2: ");
- for (int i = 0; i < MAX_PER_SQUADRA; i++) {
- System.out.println(SQUADRA2[i]);
- }
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String[] tutti = new String[MAX_GIOCATORI];
- inserisciGiocatori(tutti);
- System.out.print("Giocatori: _ ");
- stampaGiocatori(tutti);
- creaSquadre(tutti);
- }
- }
Add Comment
Please, Sign In to add comment