如何在两个输出之间添加空格?

时间:2013-10-16 00:07:16

标签: java

这是我正在使用的代码。

public void displayCustomerInfo() {
    System.out.println(Name + Income);
}

我在此代码中使用单独的main方法来调用上面的方法:

first.displayCustomerInfo();
second.displayCustomerInfo();
third.displayCustomerInfo();

有没有办法在输出之间轻松添加空格?这就是目前的情况:

Jaden100000.0
Angela70000.0
Bob10000.0

7 个答案:

答案 0 :(得分:18)

添加文字空间或标签:

public void displayCustomerInfo() {
    System.out.println(Name + " " + Income);

    // or a tab
    System.out.println(Name + "\t" + Income);
}

答案 1 :(得分:12)

如果您希望格式化得很好,可以使用System.out.printf()这样的

System.out.printf("%-20s %s\n", Name, Income);

打印如:

Jaden             100000.0
Angela            70000.0
Bob               10000.0

此格式表示:

%-20s  -> this is the first argument, Name, left justified and padded to 20 spaces.
%s     -> this is the second argument, Income, if income is a decimal swap with %f
\n     -> new line character

您还可以向Income参数添加格式,以便根据需要打印数字

查看此quick reference

答案 2 :(得分:3)

System.out.println(Name + " " + Income);

这是你的意思吗?那会在名字和收入之间留一个空格吗?

答案 3 :(得分:1)

喜欢这个吗?

 System.out.println(Name + " " + Income);

答案 4 :(得分:0)

可以在打印命令中添加

+“ \ n” +,以在下一行之后显示代码块

例如System.out.println(“ a” +“ \ n” +“ b”)在第一行中输出a,在第二行中输出b。

答案 5 :(得分:0)

代码:

class Main
{
    public static void main(String[] args)  
    {
        int a=10, b=20;
        System.out.println(a + " " + b);
    }
}

输入:无

输出:10 20

答案 6 :(得分:-1)

<input class="text-input" type="text" name="name" value="" placeholder="Username">
<input class="text-input" type="password" name="password" value="" placeholder="Password">