【问题标题】:How to mirror text in Java如何在 Java 中镜像文本
【发布时间】:2018-03-16 21:31:42
【问题描述】:

我正在尝试创建一个完整的钻石,但我几乎已经完成了所有的工作

T T
Te  Te
Tes   Tes
Test    Test
Test   Tes
Tes  Te
Te T
T

这些 for 循环是创建菱形的原因,但 spaces.substring 是我遇到困难的地方。有谁知道如何通过单独编辑 space.substring 来解决这个问题?

    for (int i = 0 ; i < len; i++)
    { 
        System.out.print(SPACES.substring(0,i));            
        System.out.print (word.substring (0,i) + System.lineSeparator()); 
        //System.out.print(SPACES.substring(i,i+1));
        System.out.print (word.substring (0,i+1));
    }  
    for (int g = len ; g >= 0; g--)
    {          
        System.out.print(SPACES.substring(0,g));           
        System.out.print (word.substring (0,g) + System.lineSeparator());              
        System.out.print (word.substring (0,g));            
    }   

如您所见,每个 for 循环的前两行创建菱形的右半部分,最后几行创建左半部分。但是左半部分没有空格子字符串,因为我不知道我会在里面放什么。

应该是这样的:

                                    H
                                   O O
                                  U   U
                                 S     S
                                E       E
                                 S     S
                                  U   U
                                   O O
                                    H

【问题讨论】:

  • System.out.print(something + System.lineSeparator()) --> System.out.println(something).
  • 您能否提供所需输出的示例?

标签: java text substring joptionpane mirroring


【解决方案1】:

代码

你可以使用方法吗?

是的:

public static void main(String[] args)
{
    new Main().printDiamond("HOUSE");
}

private void printDiamond(String word)
{
    for (int i = 0 ; i < word.length(); i++)
    {
        printPart(word, i);
    }

    for (int i = word.length() - 2; i >= 0; i--)
    {
        printPart(word, i);
    }
}

private void printPart(String word, int i)
{
    space(word.length() - (i + 1));
    System.out.print(word.charAt(i));
    if (i > 0)
    {
        space((i * 2) - 1);
        System.out.print(word.charAt(i));
    }
    space(word.length() - (i + 1));
    System.out.println();
}

private void space(int i)
{
    for (int j = 0; j < i; j++)
    {
        System.out.print(" ");
    }
}

否:

String word = "HOUSE";
String SPACES = "              ";
int len = word.length();

for (int i = 0 ; i < len; i++)
{
    System.out.print(SPACES.substring(0, len - (i + 1))); //print left padding spaces
    System.out.print(word.charAt(i)); //print left/middle character
    if (i > 0) //we don't want to do this on run 0 because there we only need to print one character in the middle (H)
    {
        System.out.print(SPACES.substring(0, (i * 2) - 1)); //print middle padding spaces
        System.out.print(word.charAt(i)); //print right character
    }
    System.out.println(SPACES.substring(0, len - (i + 1))); //optional: print right padding
}

for (int i = len - 2; i >= 0; i--) //start at len - 2. -1 because arrays start at 0 and -1 because we don't want to print the last character (E) again
{
    System.out.print(SPACES.substring(0, len - (i + 1)));
    System.out.print(word.charAt(i));
    if (i > 0)
    {
        System.out.print(SPACES.substring(0, (i * 2) - 1));
        System.out.print(word.charAt(i));
    }
    System.out.println(SPACES.substring(0, len - (i + 1)));
}

输出

    H    
   O O   
  U   U  
 S     S 
E       E
 S     S 
  U   U  
   O O   
    H    

【讨论】:

  • Stack Overflow 是一个问答网站,而不是家庭作业写作服务。请不要让用户有理由相信。谢谢。
  • 是的,您能否解释一下非方法部分发生了什么。我对此感到困惑
  • @JohnSmith 我在第一个 for 循环中添加了一些 cmets。在第二个基本一样
猜你喜欢
  • 2010-11-14
  • 2023-02-07
  • 2011-01-19
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 2016-03-07
  • 2015-07-14
相关资源
最近更新 更多