Java:将列表中的字符串围绕中心词对齐

Java:将列表中的字符串围绕中心词对齐,java,println,Java,Println,我有一个包含3个字符串的arraylist ArrayList<String> sentences = new ArrayList<String>(); sentences.add("it would be for his happiness and having some feelings himself"); sentences.add("a plan to promote the hap

我有一个包含3个字符串的arraylist

ArrayList<String> sentences = new ArrayList<String>();
                    sentences.add("it would be for his happiness and having some feelings himself");
                    sentences.add("a plan to promote the happiness of all she and Mr" );
                    sentences.add("a most unreasonable degree of happiness She must wait a moment");

                       for(String f : sentences){
                        System.out.println(f);
                        }
每个句子的中间词是“幸福”

我需要在屏幕上的输出是在形式

          it would be for his happiness and having some feelings himself 
        a plan to promote the happiness of all she and Mr 
a most unreasonable degree of happiness She must wait a moment 
其中“幸福”一词被整齐地放置在彼此下方的中心位置,句子的其余部分围绕着它。
欢迎提供任何帮助

根据您的文本,您需要做的是在单词happiness中找到H的索引,找到这些数字的最大值,然后在字符串前添加MAX currentRowHIndex空格

是的,等了一段时间后,我想做这件事,评论中提到的想法奏效了:

public class Happiness
{    
    public static void main(String[] args)
    {
        List<String> sentences = new ArrayList<String>(); //USE THE INTERFACE
        sentences.add("it would be for his happiness and having some feelings himself");
        sentences.add("a plan to promote the happiness of all she and Mr");
        sentences.add("a most unreasonable degree of happiness She must wait a moment");

        int maxHIndex = -1;
        for(String sentence : sentences) 
        {
            int hIndex = sentence.indexOf("happiness");
            if(hIndex > maxHIndex)
            {
                maxHIndex = hIndex;
            }
        }
        for(String sentence : sentences)
        {
            StringBuffer sb = new StringBuffer("");
            for(int i = 0; i < maxHIndex - sentence.indexOf("happiness") ; i++)
            {
                sb.append(" ");
            }
            System.out.println(sb.toString() + sentence);
        }
    }
}

如果有可能幸福不在句子里,那么就做一个“如果”!任何第一次尝试都是受欢迎的。如果不发布您的尝试,我们不知道您遇到了什么问题,因此我们不知道如何向您提供建议。另一方面,如果你至少不尝试先解决你能解决的问题,那你就是在欺骗自己,使自己失去了学习经验中有价值的一部分。请不要这样做。正如您从文本中看到的,您需要做的是在单词happiness中找到H的索引,找到这些数字的最大值,然后在字符串之前添加MAX currentRowHIndex空格。这就完成了。非常感谢。
public class Happiness
{    
    public static void main(String[] args)
    {
        List<String> sentences = new ArrayList<String>(); //USE THE INTERFACE
        sentences.add("it would be for his happiness and having some feelings himself");
        sentences.add("a plan to promote the happiness of all she and Mr");
        sentences.add("a most unreasonable degree of happiness She must wait a moment");

        int maxHIndex = -1;
        for(String sentence : sentences) 
        {
            int hIndex = sentence.indexOf("happiness");
            if(hIndex > maxHIndex)
            {
                maxHIndex = hIndex;
            }
        }
        for(String sentence : sentences)
        {
            StringBuffer sb = new StringBuffer("");
            for(int i = 0; i < maxHIndex - sentence.indexOf("happiness") ; i++)
            {
                sb.append(" ");
            }
            System.out.println(sb.toString() + sentence);
        }
    }
}
          it would be for his happiness and having some feelings himself
        a plan to promote the happiness of all she and Mr
a most unreasonable degree of happiness She must wait a moment