简体   繁体   中英

Printing a word in a diamond format

My assignment is to print a word in the shape of a diamond like so:

*****s
****p*p
***i***i
**d*****d
*e*******e    
r*********r
*e*******e
**d*****d
***i***i
****p*p
*****s

PS The asterisks are only there to show spacing, pretend one asterisk represent one space.

So far I have this:

public class DiamondWords
{
     public static void main(String[] args)
     {
         Scanner kbReader = new Scanner(System.in);
         System.out.print("Enter a word to be printed in diamond format: ");
         String word = kbReader.nextLine();
         int wordLength = word.length();
         for(int i = 0; i<wordLength-1; i++)
         {
             System.out.print(" ");
         }
         wordLength = wordLength - 1;
         System.out.print(word.charAt(0));
         System.out.println();
         int x =1;
         int d =1;

             for(int j =wordLength; j>0; j--)
             {  
                 wordLength = j;
                 for(int a =1; a<wordLength; a++)
                 {
                     System.out.print(" ");
                 }
                 System.out.print(word.charAt(x));
                 for(int q =0; q<d; q++)
                 {
                     System.out.print(" ");
                 }
                 d+=2;
                 System.out.print(word.charAt(x));
                 x++;
                 System.out.println();
             }
            //r*********r
            //*e*******e
            //**d*****d
            //***i***i
            //****p*p
            //*****s
         }
     }

Which prints the first half of the diamond perfectly:

    *****s
    ****p*p
    ***i***i
    **d*****d
    *e*******e    
    r*********r

The only part where I'm getting stuck is when I have to print the latter half of the diamond. Can anyone point me in the right direction? Please do not write the code for me, just try and give me some pointers based off the logic I've shown. Thank you.

Try to have only one loop. A very easy way to handle the "technicalities" of the problem is to work with an char array for the output. First you initialize it with the proper length, fill it with blanks (there is a library function for it), fill the two characters, and only then convert it to a String.

The only open question is where to put the characters, and I don't want (and should) to spoil that.

int fullLength = 2 * word.length() - 1;
for(int i = 0; i < fullLength; i++) {
    char[] line = new char[fullLength];
    Arrays.fill(line, ' ');
    int k = ???;
    char c = s.charAt(k);
    line[word.length() - 1 - k] = c;
    line[word.length() - 1 + k] = c;
    System.out.println(new String(line));
}

Obviously, you want to calculate the position "from the middle" (so we have something like word.length() - 1 +- k ), and for the first half of the word, k is equal to i .

Your task, should you decide to accept it, is to find out how to "bend k back" for the second half of the word.

import java.util.Scanner;
public class DiamondWords
{
    public static void main(String[] args)
    {
        Scanner kbReader = new Scanner(System.in);
        System.out.print("Enter a word to be printed in diamond format: ");
        String word = kbReader.nextLine();
        int wordLength = word.length();
        int wordLength2 = word.length();
        int wordSize = word.length();
        int wordLengthReverse = word.length();

        for(int i = 0; i<wordLength-1; i++)
        {
            System.out.print(" ");
        }

        wordLength = wordLength - 1;
        System.out.print(word.charAt(0));
        System.out.println();
        int x =1;
        int d =1;

        for(int j =wordLength; j>0; j--)
        {   
            wordLength = j;
            for(int a =1; a<wordLength; a++)
            {
                System.out.print(" ");
            }
            System.out.print(word.charAt(x));
            for(int q =0; q<d; q++)
            {
                System.out.print(" ");
            }
            d+=2;
            System.out.print(word.charAt(x));
            x++;
            System.out.println();
        }

        System.out.print(" " + word.charAt(wordLength2-2));
        int spaceLength =((wordLength2*2)-1) -4;
        int u =spaceLength -2;
        for(int i =0; i < spaceLength; i++)
        {
            System.out.print(" ");
        }
        System.out.print(word.charAt(wordLength2-2));
        System.out.println();

        int m=3;
        for(int num =2; num<wordSize-1; num++)
        {
            wordLength2 = num;
            for(int i =0; i<num; i++)
            {
                System.out.print(" ");
            }
            System.out.print(word.charAt(wordSize-m));
            for(int b = 0; b<u; b++)
            {
                System.out.print(" ");
            }

            System.out.print(word.charAt(wordSize-m));
            System.out.println();
            m++;
            u = u-2;
        }
        for(int r =0; r<word.length()-1; r++)
        {
            System.out.print(" ");
        }
        System.out.print(word.charAt(0));
    }
}

I have finished. This is my final code. I understand it is not as efficient as possible, or easy to follow, but it is flexible and not hard-coded, so I am content.

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