繁体   English   中英

以菱形格式打印单词

[英]Printing a word in a diamond format

我的任务是打印一个菱形的单词,如下所示:

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

PS星号仅在此处显示间距,假装一个星号表示一个空格。

到目前为止,我有这个:

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
         }
     }

完美地印出了钻石的前半部分:

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

我唯一会卡住的部分是必须打印钻石的后半部分。 谁能指出我正确的方向? 请不要为我编写代码,只需根据我所显示的逻辑尝试给我一些指针。 谢谢。

尝试只有一个循环。 处理问题的“技术性”的一种非常简单的方法是使用char数组作为输出。 首先,以适当的长度对其进行初始化,将其填充为空白(有一个库函数),填充两个字符,然后将其转换为字符串。

唯一开放的问题是在哪里放置角色,我不希望(也应该)破坏角色。

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));
}

显然,您要计算“从中间开始”的位置(所以我们有类似word.length() - 1 +- k ),对于单词的前半部分, k等于i

如果您决定接受,您的任务就是找出如何在单词的后半部分“弯曲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));
    }
}

我完成了。 这是我的最终代码。 我知道它不是尽可能高效或易于遵循,但是它很灵活并且没有硬编码,所以我很满意。

暂无
暂无

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

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