mardi 5 janvier 2016

Triangle Word Pattern using Nested Loops in Java

For this assignment, after inputting any word, it will print it in a pattern shown below (in this case, the word is computer):

       C
      O O
     M   M
    P     P
   U       U
  T         T
 E           E
RETUPMOCOMPUTER

Currently, my code is this:

    String output = "";
        for (int a = word.length()-1; a >= 1; a--)
        {
            for (int b = 0; b < word.length(); b++)
            {
            out.print(" ");
            }
        out.println(word.charAt(word.length()-1-a));
        }

        for (int c = 0; c < word.length(); c++)
        {
            out.print(word.charAt(word.length()-1-c));  
        }
        out.print(word.substring(1));   
    return output + "\n";

The output for my code currently is:

        C
        O
        M
        P
        U
        T
        E
RETUPMOCOMPUTER

Any advice or tips is much appreciated, thank you in advance!

Aucun commentaire:

Enregistrer un commentaire