2013-10-16 105 views
6

這是我正在使用的代碼。如何在兩個輸出之間添加空間?

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

我使用一個單獨的主要方法與此代碼調用上述方法:

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

有沒有一種方法可以輕鬆地輸出之間加空格?這是它目前的樣子:

Jaden100000.0 
Angela70000.0 
Bob10000.0 

回答

12

添加文字空間,或製表:

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

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

喜歡這個?

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

那是什麼意思?這會在名稱和收入之間留出空間嗎?

8

您可以使用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 

您還可以添加格式收入參數,以便根據需要打印號碼

看看這個爲quick reference

-1
import java.util.Scanner; 
public class class2 { 

    public void Multipleclass(){ 
     String x,y; 
     Scanner sc=new Scanner(System.in); 

     System.out.println("Enter your First name"); 
     x=sc.next(); 
     System.out.println("Enter your Last name"); 
     y=sc.next(); 

     System.out.println(x+ " " +y); 
    } 
} 
+0

護理闡述? – RamenChef

+0

是的肯定System.out.println(x +「」+ y); –

相關問題