Java 如何编写for循环来添加空格?(爪哇)
我必须为这段代码做一个for循环,并想知道如何在每边添加空格Java 如何编写for循环来添加空格?(爪哇),java,loops,for-loop,Java,Loops,For Loop,我必须为这段代码做一个for循环,并想知道如何在每边添加空格 //3. The method returns a String that contains width characters and centres the // text within the width by adding sufficient spaces before and after the text. //If the total number of spaces required to centre the text
//3. The method returns a String that contains width characters and centres the
// text within the width by adding sufficient spaces before and after the text.
//If the total number of spaces required to centre the text(i.e the total of spaces before and after)
//is an odd number the additional space should be on the left of the text. If the text is longer/wider
//the method should return only the leftmost characters of the text(i.e. it should trim text text).
public static String centre(String text, int width){
int padding = width - text.length() / 2;
if(padding == 0)
这就是我到目前为止所做的您可以在循环中附加到
StringBuilder
:
公共静态字符串中心(字符串文本,整数宽度){
int diff=width-text.length();
如果(diff您可以这样做。此方法使用StringBuilder
来包含文本。不需要检查文本何时与宽度匹配,因为如果出现这种情况,循环将不会接合。文本周围用单引号表示间距
String text = "no spaces needed";
System.out.printf("'%s'%n", centre(text,text.length()));
text = "even on both sides";
System.out.printf("'%s'%n", centre(text,text.length()+10));
text = "extra space on left";
System.out.printf("'%s'%n", centre(text,text.length()+11));
text = "this text is too long";
System.out.printf("'%s'%n", centre(text,text.length()-5));
印刷品
'no spaces needed'
' even on both sides '
' extra space on left '
'this text is too'
public static String centre(String text, int width){
int len = text.length();
// automatically adjusts for odd number of spaces on left and even on right
int left = (width-len+1)/2;
int right = width-len-left;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < left; i++) {
sb.append(' ');
}
sb.append(text);
for (int i = 0; i < right; i++) {
sb.append(' ');
}
// return the string or a right trimmed string if the string exceeds the width.
return sb.substring(0,width);
}
“不需要空格”
“即使是双方”
“左侧的额外空间”
“此文本太过”
公共静态字符串中心(字符串文本,整数宽度){
int len=text.length();
//自动调整左侧的奇数空格和右侧的偶数空格
int left=(宽度len+1)/2;
int right=宽度len left;
StringBuilder sb=新的StringBuilder();
for(int i=0;i
试试这个
公共静态字符串中心(字符串文本,整数宽度){
//计算左填充的最大填充长度。
int overwall_padding=width-text.length();
int padlen=(覆盖填充>>1)+(宽度和1);
//将填充创建为字符数组。
char[]padding=新字符[padlen];
//用空格填充数组
对于(int i=0;i>1);
//返回
返回builder.toString();
}
请注意,重复使用char[]
缓冲区只是为了在保持效率的同时优化速度。您可能会发现这也很有趣。我已经应用了String.format()来生成填充。str.substring()用于修剪()
语法:
String.format("%[L]s", str);
范例
String.format("%3s", ""); // Generates 3 spaces
String.format("%5s", ""); // Generates 5 spaces
String.format("%3s%s%3s", "", "Hello", ""); // ***Hello*** ; *=space
参考:
代码:
输出(*用于可视化空间):
OP说要求用于循环。@WJS我可能有点遗漏了,但我纠正了它。
class Solution{
public static String centre(String text, int width){
int length = text.length();
if(length >= width)
return text.substring(0, width); // trimming to width
else{
int padding = width - length;
if(padding == 1)
return String.format(" %s", text); // adding single space to the left
else{
int padLeft, padRight;
padLeft = padRight = padding >> 1; // dividing by 2
if((padding & 1) == 1) // if odd number the adding extra 1 to left
padLeft += 1;
return String.format("%" + padLeft + "s%s%" + padRight + "s", "", text, "");
}
}
}
public static void main(String[] args){
System.out.println(centre("Hello", 4));
System.out.println(centre("Hello", 5));
System.out.println(centre("Hello", 6));
System.out.println(centre("Hello", 7));
System.out.println(centre("Hello", 8));
}
}
Hell Hello
*Hello
*Hello*
**Hello*