Announcement

Collapse
No announcement yet.

Probleme mit return!!!

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Probleme mit return!!!

    Hallo!

    Ich habe grundsätzlich ein Problem mit dem return-Befehl.
    Worauf kommt es an auf was muss geachtet werden??

    Dieses Programm funzt z.B nicht

    class Sortieren
    {

    public static int[] bubblesort()
    {
    int[] abc = {4,3,7,1,2,9,5};
    int zwischenspeicher;
    for ( int i=0; i<= 6; i++ ) {
    for ( int j=i+1; j<= 7; j++ ) {
    if ( abc[j]<abc[i] ) {
    zwischenspeicher = abc[j];
    abc[j] = abc[i];
    abc[i] = zwischenspeicher;
    System.out.println(abc);


    }
    }
    }
    return bubblesort();

    }

    public static void main(String[] args)
    {

    System.out.println(bubblesort());
    }
    }

    Vielleicht kann mich jemand aufklären.
    Grüße

  • #2
    1. der return wert sollte "abc" sein und nicht bubblesort()!!!
    2. die ausgabe des arrays mittels System.out.println(...) bringt nicht
    wirklich, da leider nur die speicher adresse des arrays ausgegeben
    wird

    hier ein vorschlag zur verbesserung:

    <pre>
    public class Sortieren {

    public static int[] bubblesort() {
    int[] abc = {4,3,7,1,2,9,5};
    int zwischenspeicher;
    for ( int i=0; i<= 6; i++ ) {
    for ( int j=i+1; j< 7; j++ ) {
    if ( abc[j]<abc[i] ) {
    zwischenspeicher = abc[j];
    abc[j] = abc[i];
    abc[i] = zwischenspeicher;
    printArray(abc);
    }
    }
    }
    return abc;
    }

    public static void printArray( int[] array ){
    String ausgabe = "";
    for( int i=0; i < array.length; i++){
    ausgabe += array[i]+" ";
    }
    System.out.println( ausgabe );
    }

    public static void main(String[] args) {

    int[] returnValue = bubblesort();
    System.out.println("return: ");
    printArray( returnValue );
    }
    }
    </pre&gt

    Comment

    Working...
    X