英文:
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
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<String> 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));
}
}
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<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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论