是这种效果吗?
public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in))
String num = br.readLine()
配粗游int aLen = ((num.length())%4==0) ? num.length()/4: num.length()/4+1
String[] a = new String[aLen]
String result = ""凳岩
for(int i = 0 i < aLen i++) {
int begin = i * 4
培销 int end = (i*4+4) > num.length() ? num.length() : i * 4 + 4
a[i] = num.substring(begin, end)
}
for(int i = 0 i < aLen i++) {
result = result + a[i] + " "
}
System.out.println(result)
}
定义一个char型的宴粗数组,然后循环这个数组并做判断,如果条件成立,那么用取出来的每个厅判字符后加一个空格输出:
public class Test {public static void main(String[] args) {
String t = args[0]
char[] charArray = t.toCharArray()
晌伏镇 if( null != charArray && charArray.length > 0){
for ( char tempChar : charArray)
System.out.print(tempChar + " ")
}
}
}
%4.xx 使输出右对齐,不足4位用空格;
%-10s 使字符串左对齐输出,不足10位和或空,后加空格;
%10s 使字符串右对齐输出,不足10位,前加空格。
输出的对齐格式:printf("%4.d\n",1)
printf("%4.d\n%4.d\n%4.d\n",3,12,1123)
printf("%-10s%d\n",s,12)
%4.xx 使输出右对齐;不足4位用空格;
%.4xx 使小数保留4位小数,若该数为整形,不足四位前填0;多余4位原样输出;
%-4.xx 使输出的数左对齐;
%-10s 使字符串左对齐输出,不足10位,后加空格;
%10s 使字符串右对齐输出,不足10位,前加空格;
%10.s 不输出字符,只输出10位空格;
%.10s 字符原样输出;
举例:
#include<stdio.h>
int main()
{
printf("%4.d%4.d%4.d\n",1,34,12344)
printf("%4.d\n%4.d\n%4.d\n",3,12,11123) //右唤瞎对齐;不足四位,前加空格;
printf("%.4d\n%.4d\n%.4f\n%.4f\n",23,2342,2.3,343.32) //对小数,保留四位小数;对整数,不足四位,前加零;
printf("%-4.d%-4d\n",1,23423) //左对齐;不足四位,后加空格;
printf("%-10s%-10s\n","asdsd","asdsfdgfhgh"团仔) //不足十位,后加空格;
printf("%10s%10s\n","sdsfsf","sdsfdsgsgf") //不足十位,前加空格;
printf("%10.sq\n","asdaff") //输出十个空格;
printf("%.10s%.10s\n","asdad","sdsfdsgfgg") //字符串原样输出;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)