简体   繁体   English

以菱形格式打印单词

[英]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. PS星号仅在此处显示间距,假装一个星号表示一个空格。

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. 处理问题的“技术性”的一种非常简单的方法是使用char数组作为输出。 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 . 显然,您要计算“从中间开始”的位置(所以我们有类似word.length() - 1 +- k ),对于单词的前半部分, k等于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. 如果您决定接受,您的任务就是找出如何在单词的后半部分“弯曲k ”。

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. 我知道它不是尽可能高效或易于遵循,但是它很灵活并且没有硬编码,所以我很满意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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