Trace-Tabelle erstellen

Hallo,
ich bin völlig neu in der Welt des Programmierens gelandet und habe direkt ein Problem... Für die Uni soll ich eine Trace-Tabelle mit dem modernen und klassischen Euklidischen Algorithmus erstellen. Ich habe auch schon jeweils ein Programm geschrieben, jedoch bekomme ich nur eine Zahl anstatt der Tabelle raus. Hoffentlich könnt ihr mir weiterhelfen. Als Hinweis zur Fromatierung wurde gesagt, wir könnten das Tabulatorzeichen \t verwenden. Allerdings weiß ich nicht, wo ich das brauche und ob ich das brauche.


Java:
public class EuklidKlassisch {

    public static void main( String[] args ){
    int x = Integer.parseInt(args[0]);        // Wertuebernahme von ...
    int y = Integer.parseInt(args[1]);       // ... der Kommandozeile
    if ( x <= 0 || y <= 0)  {                      // '||' steht fuer 'oder'
        System.out.println("nur positive Argumente!!");
        return; // Ende
    }
   
    // ggT(x,y)                                    // K1
    while ( y != 0 ) {                             // K2
        if ( x > y )                               // K3
        x = x - y;                             // K4
        else
        y = y - x;                             // K5
    }
    System.out.println(x);                         // K6

    return;
    }
}


public class EuklidModern {
   
    public static void main( String[] args ){
    int x = Integer.parseInt(args[0]);     // Wertuebernahme von ...
    int y = Integer.parseInt(args[1]);    // ... der Kommandozeile
    if ( x <= 0 || y <= 0)  {                   // '||' steht fuer 'oder'
        System.out.println("nur positive Argumente!!");
        return; // Ende
    }
   
    // ggT(x,y)                                    // M1
    int r;
    while ( y != 0 ) {                             // M2
        r = x % y;                                 // M3
        x = y;                                     // M4
        y = r;                                     // M5
    }
    System.out.println(x);                         // M6

    return;
    }
}
 
Zuletzt bearbeitet von einem Moderator:
Na, so (ist ganz einfach, lediglich die Tabellenausgabeanweisungen hinzufügen):

Java:
public class Euklid {
    public static void main(String[] args) {
        System.out.println("Ergebnis: " + klass(40, 38));
        System.out.println("Ergebnis: " + modern(40, 38));
    }

    public static int klass(int x, int y) {
        if (x <= 0 || y <= 0) {
            System.out.println("nur positive Argumente!!");
            return -1;
        }

        System.out.println(x + "\t" + y);

        // ggT(x,y)  // K1
        while (y != 0) { // K2
            if (x > y) { // K3
                x = x - y; // K4
            } else {
                y = y - x; // K5
            }

            System.out.println(x + "\t" + y);
        }
        return x;
    }

    public static int modern(int x, int y) {
        if (x <= 0 || y <= 0) {
            System.out.println("nur positive Argumente!!");
            return -1;
        }

        System.out.println(x + "\t" + y);

        // ggT(x,y)  // M1
        int r;
        while (y != 0) { // M2
            r = x % y; // M3
            x = y; // M4
            y = r; // M5

            System.out.println(x + "\t" + y);
        }

        return x;
    }
}

Dann kann man super schnell erkennen, weshalb der Moderne seinem Namen alle Ehre macht:

Code:
40    38
2    38
2    36
2    34
2    32
2    30
2    28
2    26
2    24
2    22
2    20
2    18
2    16
2    14
2    12
2    10
2    8
2    6
2    4
2    2
2    0
Ergebnis: 2
40    38
38    2
2    0
Ergebnis: 2
 

Neue Themen


Zurück
Oben