java中如何实现格式字符的输出?
就像C中printf("%2d",s);输出的结果就会右对齐前面空一空格。
请问这个问题在java中如何实现,本人初学java。

解决方案 »

  1.   

    这么说吧、、我想输出数字2和数字10,输出的结果像这样
     2
    10
    在2前面加以空格,
    这个在C中在printf("%2d");在d前面加个2就行了,在java中如何实现。。
      

  2.   

    为了让C程序员快速适应java的开发,保留很多C的东西。
    java有printf。在这个PrintStream类下面,有该方法。你可以自己去查一下Api
      

  3.   

    System.out.print("2"+" "+"10");估计你写的是System.out.println("2"+" "+"10");
      

  4.   

    printfpublic PrintStream printf(String format,
                              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
      

  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();
    }
    }
      

  6.   

    至于吗? System.out就是一个PrintStream