Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

can you help me with my java problem?

when i run it it come out "ABCDE"

i want it like this "AEDCB"

this is what i currently have

public class recursion2

{

public static void main(String [] args)

{

char a [] = {'A', 'B','C','D','E'};

System.out.println(a);

Switch(a, 2,4);

}

public static void Switch(char a [], int b1, int b2)

{

char tmp;

if (b1 <= b2)

return;

else

{

tmp = a[b1];

a[b1] = a[b2];

a[b2] = tmp;

b1++;

b2--;

Switch(a, b1, b2);

}

}

}

2 Answers

Relevance
  • 7 years ago
    Favorite Answer

    Try this:

    public class OmarsRecursion {

    public static void main(String [] args) {

    char[] a = {'A', 'B', 'C', 'D', 'E'};

    System.out.println(a);

    Switch(a, 1, 4);

    }

    public static void Switch(char[] a, int b1, int b2) {

    char tmp;

    if (b1 >= b2) {

    System.out.println (a);

    return;

    } else {

    tmp = a[b1];

    a[b1] = a[b2];

    a[b2] = tmp;

    b1++;

    b2--;

    Switch(a, b1, b2);

    }

    }

    }

  • Pipsy
    Lv 5
    7 years ago

    Firstly don't use return in a void function, secondly you need to relook at the your first if statement it says:

    if (b1 <= b) {

    do this..

    }

    and your b1 = 2 and your b2 = 4 so you tell me what's wrong?

Still have questions? Get your answers by asking now.