有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在java中的一种方法中居中、左右填充对齐

我最近开始学习java,在我的java类中,我必须创建一个方法,在主java类中打印文本,文本向左、居中、向右。但是,我想在不使用printf的情况下执行此操作

这是我的文字:

MyJavaUtils.formatText("This is my text left justified", 'l', 38);
MyJavaUtils.formatText("This is my text right justified", 'r', 38);
MyJavaUtils.formatText("This is my text centred", 'c', 38);
System.out.println("----------------------------------------");
MyJavaUtils.formatText("This is also left justified", 'l', 38);
MyJavaUtils.formatText("This is also right justified", 'r', 38);
MyJavaUtils.formatText("This is also centred", 'c', 38);
System.out.println("----------------------------------------");
MyJavaUtils.formatText("Also left", 'l', 18);
MyJavaUtils.formatText("Also right", 'r', 18);
MyJavaUtils.formatText("Also centred", 'c', 18);
System.out.println("----------------------------------------");

到目前为止,我的方法是:

public static void formatText(String text, char justified, int wide) {
        int space = wide - text.length();
        for (int num = 0; num < space; ++num) {
            System.out.print(" ");
        }
        System.out.println(text);
    }
}

但是,此代码将每行文本打印到右侧。额外的空间量是正确的,但我不希望每一行都打印到右边。就像文本所建议的,文本左对齐应该是左对齐,右对齐,居中就是居中。我不确定如何用一种方法完成所有这些

输出应如下所示:

>This is my text left justified        <
>       This is my text right justified<
>       This is my text centred        <

然而,我不太确定如何在不使用printf的情况下打印到左边、右边和中间。我被告知要通过查找文本的字符串长度来实现这一点,我已经知道了。但是,我不知道如何应用它打印出适当数量的空格,以便将输出填充到正确的字符数。当我做中心输出时,如果额外的空间量是奇数,我应该在右边加1个空格

任何帮助或提示都将不胜感激,谢谢


共 (3) 个答案

  1. # 1 楼答案

    要使文本居中,应该在字符串的左侧附加left = (wide - text.length()) / 2空格,在右侧附加wide - text.length() - left空格

  2. # 2 楼答案

    您只需要将另外两个案例添加到该方法中

    public static void formatText(String text, char justified, int wide) {
            int space = wide - text.length();
            switch(justified){
              case 'r':
                for (int num = 0; num < space; ++num) {
                    System.out.print(" ");
                }
                System.out.println(text);
                break;
             case 'l':
               System.out.print(text);
               for (int num = 0; num < space; ++num) {
                    System.out.print(" ");
               }
               System.out.println();
               break;
             case 'c':
               for(int num = 0;num < (wide - text.length())/2 ; ++num){
                  System.out.print(" ");
               }
               System.out.print(text);
               for(int num = 0;num < (wide - text.length())/2 ; ++num){
                  System.out.print(" ");
               }
               System.out.println();
               break;
           }
        }
    }
    
  3. # 3 楼答案

    我认为这种方法可以满足你的需要

    public static void formatText(String text, char justified, int wide)
    {
        int nOfSpaces = wide - text.length();
        String spaces = new String();
        String result = new String();
    
        switch (justified)
        {
            case 'r':
                spaces = new String(new char[nOfSpaces]).replace('\0', ' ');
                result = spaces + text;
                break;
            case 'l':
                spaces = new String(new char[nOfSpaces]).replace('\0', ' ');
                result = text + spaces;
                break;
            case 'c':
                int lxNOfSpaces = nOfSpaces / 2;
                int rxNOfSpaces = (nOfSpaces % 2) == 0 ? lxNOfSpaces : lxNOfSpaces + 1;
    
                String lxSpaces = new String(new char[lxNOfSpaces]).replace('\0', ' ');
                String rxSpaces = new String(new char[rxNOfSpaces]).replace('\0', ' ');
    
                result = lxSpaces + text + rxSpaces;
                break;
        }
        System.out.println(result);
    }