Guest User

Untitled

a guest
Jan 19th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2. import java.util.Random;
  3.  
  4. public class creaSquadre {
  5.  
  6.     private static final int MAX_GIOCATORI = 10;
  7.     private static final int MAX_PER_SQUADRA = 5;
  8.     private static String[] SQUADRA1 = new String[MAX_PER_SQUADRA];
  9.     private static String[] SQUADRA2 = new String[MAX_PER_SQUADRA];
  10.     private static int j = 0, k = 0;
  11.  
  12.     // private static String[] SQUADRA_1 = new String[MAX_PER_SQUADRA];
  13.     // private static String[] SQUADRA_2 = new String[MAX_PER_SQUADRA];
  14.  
  15.     private static void inserisciGiocatori(String[] giocatori) {
  16.  
  17.         for (int i = 0; i < MAX_GIOCATORI; i++) {
  18.             giocatori[i] = JOptionPane.showInputDialog("inserisci il "
  19.                     + (i + 1) + "° giocatore:");
  20.         }
  21.     }
  22.  
  23.     private static void stampaGiocatori(String[] giocatori) {
  24.         for (int i = 0; i < MAX_GIOCATORI; i++)
  25.             System.out.print(giocatori[i] + " _ ");
  26.         System.out.println(" ");
  27.     }
  28.  
  29.     private static boolean isPieno(String[] squadra) {
  30.         boolean pieno = false;
  31.         for (int i = 0; i < MAX_PER_SQUADRA; i++) {
  32.             if (squadra[i] == null)
  33.                 break;
  34.             else
  35.                 pieno = true;
  36.         }
  37.         return pieno;
  38.     }
  39.  
  40.     private static void creaSquadre(String[] giocatori) {
  41.  
  42.         Random r = new Random();
  43.  
  44.         for (int i = 0; i < MAX_GIOCATORI; i++) {
  45.             int a = r.nextInt(2);
  46.             switch (a){
  47.             case 0:{
  48.                 if (giocatori[i]!=SQUADRA1[j]){
  49.                     SQUADRA1[j]=giocatori[i];
  50.                     j++;
  51.                 } else if(giocatori[i]== SQUADRA1[j]){
  52.                     SQUADRA2[k]=giocatori[i];
  53.                     k++;
  54.                 } else if(giocatori[i]==SQUADRA1[j] && giocatori[i]==SQUADRA2[k]){
  55.                     creaSquadre(giocatori);
  56.                 }
  57.             }
  58.             case 1: {
  59.                 if (giocatori[i]!=SQUADRA2[k]){
  60.                     SQUADRA2[k]=giocatori[i];
  61.                     k++;
  62.                 } else if(giocatori[i]== SQUADRA2[k]){
  63.                     SQUADRA1[j]=giocatori[i];
  64.                     j++;
  65.                 } else if(giocatori[i]==SQUADRA1[j] && giocatori[i]==SQUADRA2[k]){
  66.                     creaSquadre(giocatori);
  67.                 }
  68.             }
  69.             }
  70.            
  71.         }
  72.         System.out.println("squadra 1: ");
  73.         for (int i = 0; i < MAX_PER_SQUADRA; i++) {
  74.             System.out.println(SQUADRA1[i]);
  75.         }
  76.         System.out.println("squadra 2: ");
  77.         for (int i = 0; i < MAX_PER_SQUADRA; i++) {
  78.             System.out.println(SQUADRA2[i]);
  79.         }
  80.     }
  81.  
  82.     /**
  83.      * @param args
  84.      */
  85.     public static void main(String[] args) {
  86.         // TODO Auto-generated method stub
  87.         String[] tutti = new String[MAX_GIOCATORI];
  88.  
  89.         inserisciGiocatori(tutti);
  90.         System.out.print("Giocatori: _ ");
  91.         stampaGiocatori(tutti);
  92.         creaSquadre(tutti);
  93.     }
  94.  
  95. }
Add Comment
Please, Sign In to add comment