java 以金字塔形式打印字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15228457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:55:24  来源:igfitidea点击:

Printing a String in pyramid Form

javastring

提问by christopher

I like to print a string that is got as an input from the user in a Pyramid form letter by letter. For example - When a user gives an input as "string" then the output should be:

我喜欢以金字塔形式一个字母一个字母地打印作为用户输入的字符串。例如 - 当用户将输入作为“字符串”提供时,输出应该是:

     s
    s t
   s t r
  s t r i
 s t r i n
s t r i n g

I've tried a small program but it doesn't arranges the words in Pyramid form. The program is

我试过一个小程序,但它没有以金字塔形式排列单词。该程序是

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

    public class Mainer {
    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();
           for(int i=0; i<l;i++)
           {

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

And the output of the above program is(when string is given as input) Enter the String : string

上面程序的输出是(当字符串作为输入时)输入字符串:字符串

s
s t
s t r
s t r i
s t r i n
s t r i n g

Can you make it arranged as the first example?

你能把它安排成第一个例子吗?

采纳答案by Chris Kdon

You need to add the half the length of the string as number of spaces (padding) in front of the string before you output it. But subtracting 1 space each time as you iterate in order to create the shape.

在输出之前,您需要在字符串前面添加字符串长度的一半作为空格数(填充)。但是每次迭代时都会减去 1 个空格以创建形状。

Or another way to say it is that you're getting the length of the original string and printing out the number of spaces for the characters you didn't print.

或者另一种说法是,您正在获取原始字符串的长度并打印出未打印字符的空格数。

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

回答by christopher

So what your program needs to do is pad out the text with spaces. In your first example, your first output is actually one letter, but in the same position as the middle letter of the last output.

所以你的程序需要做的是用空格填充文本。在您的第一个示例中,您的第一个输出实际上是一个字母,但与最后一个输出的中间字母位置相同。

So the first output in pseudo-code would look something like:

所以伪代码中的第一个输出看起来像:

String padding = (text.length/2) number of spaces;
// The padding on the left.
Print padding
// The letter to print.
Print first letter

Keep in mind that the length of the padding will change with the length of the text you are outputting in that iteration.. but I'm not going to tell you that. That would ruin all the fun :)

请记住,填充的长度将随着您在该迭代中输出的文本的长度而变化.. 但我不会告诉您这一点。那会毁了所有的乐趣:)

回答by Kent

is this cheating? (only one loop)

这是作弊吗?(只有一个循环)

String s = "string";
        int len = s.length();           
        String tmp = "";
        for (char c : s.toCharArray()) {
            tmp += tmp.length() > 0 ? " " + String.valueOf(c) : String.valueOf(c);
            System.out.printf("%" + (len + tmp.length() - 1) + "s\n", tmp);
            len--;
        }

output:

输出:

     s
    s t
   s t r
  s t r i
 s t r i n
s t r i n g

回答by Sam I am says Reinstate Monica

just write another for loop to print spaces

只需编写另一个 for 循环来打印空格

for(int i=0; i<l;i++)
{

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

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

for the record, you might have to tweak this.

为了记录,您可能需要对此进行调整。

回答by laguille

That should do the trick

这应该够了吧

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

public class Mainer {
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();
       for(int i=0; i<l;i++)
       {
           int padding = s.length() - i;
           if (padding> 0) {
               System.out.printf("%" + padding + "s", " ");
           }
           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);
   }
}
}