简体   繁体   中英

Java reverse triangle string

Hello I want a string displayed as a reverse triangle. I cannot get it to display when I use the reverse method. Clearly I am doing it wrong.

for (int i= 1; i >= 1; i--) { //reverse here
*******************
 *****************
  ***************
   *************
    ***********
     *********
      *******
       *****
        ***
         *

I want to display a string in this shape above but it needs to display a word

import java.io.*;
import java.util.Scanner;
import java.lang.String;

public class WorkShop2ABPaola
{
    /**
     * @param args the command line arguments
     */
    public static void main(String args [])
    {
        try
        {
            Scanner sc = new Scanner(System.in);
            String s;
            int l;
            System.out.println("Enter the String : ");
            s = sc.nextLine();
            l = s.length();
            //outer loop
            for(int i = 0; i < l; i++)
            {
                int padding = s.length() - i;
                if(padding > 0)
                {
                    System.out.printf("%" + padding + "s", " ");

                    //for inner loop
                }
                for(int j = 0; j < i; j++)
                {
                    System.out.printf("%c ", s.charAt(j));
                }
                System.out.printf("%c\n", s.charAt(i));
            }
        }
        catch(Exception e)
        {
            System.err.println(e);
        }
    }
}

The first line you posted is the right way to go: Start at the length (minus one, because the length index starts at zero) and then move down. So instead of starting i at zero, start it at l (The length)

for(int i = l-1; i > -1; i--) {
     int padding = s.length() - i;
     if(padding > 0)
     {
          System.out.printf("%" + padding + "s", " ");

         //for inner loop
      }
      for(int j = 0; j < i; j++)  {
            System.out.printf("%c ", s.charAt(j));
      }
      System.out.printf("%c\n", s.charAt(i));
}

Output: (With "CorrectOutput" as the given String )

 C o r r e c t O u t p u t
  C o r r e c t O u t p u
   C o r r e c t O u t p
    C o r r e c t O u t
     C o r r e c t O u
      C o r r e c t O
       C o r r e c t
        C o r r e c
         C o r r e
          C o r r
           C o r
            C o
             C

You had it mostly correct. You will only have l/2 lines because you are adding a space of padding on both sides each time.

This should work:

import java.io.*;
import java.util.Scanner;
import java.lang.String;

public class WorkShop2ABPaola
{
    /**
     * @param args the command line arguments
     */
    public static void main(String args [])
    {
        try
        {
            Scanner sc = new Scanner(System.in);
            String s;
            int l;
            System.out.println("Enter the String : ");
            s = sc.nextLine();
            l = s.length();
            //outer loop
            for(int i = 0; i < l/2; i++)
            {
                int padding = i;
                for (int j = 0; j < padding; j++)
                {
                    System.out.print(' ');
                }
                for(int k = 0; k < l-(padding*2); k++)
                {
                    System.out.print(s.charAt(k));
                }
                System.out.println();
            }
        }
        catch(Exception e)
        {
            System.err.println(e);
        }
    }
}

Output generated by "TestString":

TestString
 TestStri
  TestSt
   Test
    Te

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM