33cloud33 / Progetti

Programmi vari, progetti e altro
1 stars 1 forks source link

Esercizio 12 (RoundTable) non funzionante #1

Closed 33cloud33 closed 6 years ago

33cloud33 commented 6 years ago

Il programma restituisce un ArrayIndexOutOfBoundsException con valore 0.

GabrieleL18 commented 6 years ago

Sistemato

/*
 * Modulo Josephus:
 *
 * Programma per risolvere il problema di Giuseppe Flavio
 * (metodi statici))
 *
 * Ultimo aggiornamento: 10/04/2018
 */
public class Josephus {

    public static int josephus( int n ) {

        return josephusRec( new RoundTable(n) );
    }

    private static int josephusRec( RoundTable rt ) {

        if ( rt.numberOfKnightsIn() > 1 ) {

            return josephusRec( rt.afterNextKnightQuits() );
        } else {

            return rt.knightWithJugIn();
        }
    }

    public static void main( String args[] ) {

        int n = 10;
        //Soluzione 10
        System.out.println("La soluzione se abbiamo " + n + " cavalieri è " + josephus(n));
        //Soluzione per da 1 a 10
        System.out.println("Soluzioni da 1 cavaliere fino a " + n + " cavalieri");
        for ( int k=1; k<=n; k=k+1 ) {
            System.out.println( k+ "    " + josephus(k) );
        }
    }

}  // class Josephus
33cloud33 commented 6 years ago

@GabrieleL18 Grazie, aggiunto in "testing"!