How would I use printf() and a String array of inputed words to print out within a box of asterisks

huangapple go评论59阅读模式
英文:

How would I use printf() and a String array of inputed words to print out within a box of asterisks

问题

在这种格式中,'Nivla' 将位于字符串列表内,用户可以提供输入进行格式化。


  • Nivla *


此外,方框的大小将随着字符串数组的行数增加而增加。例如:["Nivla is","not intelligent"];


  •  Nivla is      *
    
  • not intelligent *


另外,我在尝试像上面那样使字符串居中时遇到了问题。

我目前使用的代码是:

public void run() {
Scanner s = new Scanner(System.in);
ArrayList inputs = new ArrayList<>();
String input;
do {
input = s.nextLine();
inputs.add(input);
} while(!input.equals(""));
printBox(inputs);
}

public void printBox(ArrayList inputs) {
    for (int i = 0; i < inputs.size(); i++) {
        System.out.printf("*\t%s\t*\n", inputs.get(i));
    }
}

有没有办法解决我的问题?
英文:

In this format where 'Nivla' would be within a String List, and input is available to be formatted by the user.

************
*   Nivla  *
************

Also, the box would increase in size with the lines of the string array. E.g. ["Nivla is" , "not intelligent"];

**********************
*      Nivla is      *
*   not intelligent  *
**********************

Also, I'm having trouble centering the string around the center like above.

The code I'm current using is:

public void run() {
        Scanner s = new Scanner(System.in);
        ArrayList&lt;String&gt; inputs = new ArrayList&lt;&gt;();
        String input;
        do {
            input = s.nextLine();
            inputs.add(input);
        } while(!input.equals(&quot;&quot;));
        printBox(inputs);
    }

    public void printBox(ArrayList inputs) {
        for (int i = 0; i &lt; inputs.size(); i++) {
            System.out.printf(&quot;*\t%s\t*\n&quot;, inputs.get(i));
        }
    }

Is there any way to solve my issue?

答案1

得分: 1

你需要根据最长输入的长度来确定空格的数量,而不仅仅是在每一侧打印制表符。您还应该在printBox方法中添加一个填充参数,以指定在单词的每一侧放置多少空格。

以下是一个相当不错的解决方案:

    int max = 0;
    int padding = 10;
    public void run() {
        Scanner s = new Scanner(System.in);
        ArrayList<String> inputs = new ArrayList<>();
        String input;
        do {
            input = s.nextLine();
            inputs.add(input);
            max = Math.max(max, input.length());
        } while(!input.equals(""));
        printBox(inputs, padding);
    }

    public void printBox(ArrayList inputs, int padding) {
        printStars();
        // go to size - 1 because the last input is always ""
        for (int i = 0; i < inputs.size() - 1; i++) {
            int len = ((String)inputs.get(i)).length();
            int frontPad = (max + len) / 2 + padding;
            // need to round or else sometimes the padding will be one too short
            int backPad = padding + (int)Math.round(((max - len) / 2.0));
            System.out.printf("*%" + frontPad + "s%" + backPad + "s\n", inputs.get(i), "*");
        }
        printStars();
    }

    private void printStars() {
        for (int i = 0; i <= max + padding * 2; i++) {
            System.out.print("*");
        }
        System.out.println();
    }
英文:

You need to make the amount of spaces dependent on the length of the longest input instead of just printing tabs on each side. You should also add a padding parameter in your printBox method to specify how many spaces to put on each side of the word.

Here is one possible solution that works pretty decently:

    int max = 0;
int padding = 10;
public void run() {
Scanner s = new Scanner(System.in);
ArrayList&lt;String&gt; inputs = new ArrayList&lt;&gt;();
String input;
do {
input = s.nextLine();
inputs.add(input);
max = Math.max(max, input.length());
} while(!input.equals(&quot;&quot;));
printBox(inputs, padding);
}
public void printBox(ArrayList inputs, int padding) {
printStars();
// go to size - 1 because the last input is always &quot;&quot;
for (int i = 0; i &lt; inputs.size() - 1; i++) {
int len = ((String)inputs.get(i)).length();
int frontPad = (max + len)/2 + padding;
// need to round or else sometimes the padding will be one too short
int backPad = padding + (int)Math.round(((max - len)/2.0));
System.out.printf(&quot;*%&quot; + frontPad + &quot;s%&quot; + backPad + &quot;s\n&quot;, inputs.get(i), &quot;*&quot;);
}
printStars();
}
private void printStars() {
for (int i = 0; i &lt;= max + padding*2; i++) {
System.out.print(&quot;*&quot;);
}
System.out.println();
}

huangapple
  • 本文由 发表于 2020年9月10日 03:19:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63818210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定