java中如何实现格式字符的输出?
就像C中printf("%2d",s);输出的结果就会右对齐前面空一空格。
请问这个问题在java中如何实现,本人初学java。
就像C中printf("%2d",s);输出的结果就会右对齐前面空一空格。
请问这个问题在java中如何实现,本人初学java。
解决方案 »
- 求救:windows环境下无法运行JAR包下的class文件
- 关于包中的类的导入问题,求助~~~~谢谢
- 困扰我一整天了,不知道为什么,ObjectOutputStream的writeObject()老是出错!!!
- 求java 2 SDK
- 关于从数据库取数据写入文本文件……
- 有点好玩的中文编程。问:如何将字符(如中文名)赋给姓名?
- 如何获取 JAVA Swing控件字体的颜色?
- 分不多,没有可用分了,,ECLIPSE中为什么不可以指定新建的类继承自某一个类,我的只有一个object,,怎么回事?
- 流问题
- oracle数据库中数据导出到一个文本文件中,每一条记录的换行问题
- 求助 java数组类型转化
- java写的程序,如何测试其占用带宽
2
10
在2前面加以空格,
这个在C中在printf("%2d");在d前面加个2就行了,在java中如何实现。。
java有printf。在这个PrintStream类下面,有该方法。你可以自己去查一下Api
Object... args)
A convenience method to write a formatted string to this output stream using the specified format string and arguments.
An invocation of this method of the form out.printf(format, args) behaves in exactly the same way as the invocation out.format(format, args)
Parameters:
format - A format string as described in Format string syntax
args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by the Java Virtual Machine Specification. The behaviour on a null argument depends on the conversion.
Returns:
This output stream
Throws:
IllegalFormatException - If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.
NullPointerException - If the format is null
Since:
1.5
package classdemo;import java.io.PrintStream;public class PrintStream_Class_Demo { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PrintStream ps = new PrintStream(System.out);
int i = 2;
int j = 10;
ps.printf(" %d\n%d", i, j);
ps.close();
}
}