2013-10-16 11 views
6

To jest kod, nad którym pracuję.Jak dodać spację między dwoma wyjściami?

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

używam oddzielnego główną metodę z tego kodu, aby wywołać metodę powyżej:

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

Czy istnieje sposób, aby łatwo dodać przestrzenie pomiędzy wyjściami? To, co w tej chwili wygląda następująco:

Jaden100000.0 
Angela70000.0 
Bob10000.0 

Odpowiedz

12

Dodaj dosłownego miejsca lub kartę:

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

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

Jak to?

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

że to, co masz na myśli? To spowoduje odstęp między nazwą a dochodem?

8

Można użyć System.out.printf() takiego, jeśli chcesz, aby ładnie sformatowany

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

Drukuje jak:

Jaden    100000.0 
Angela   70000.0 
Bob    10000.0 

Format ten oznacza:

%-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 

Można też dodać formatowanie do argumentu dochodu, aby numer był drukowany zgodnie z wymaganiami

Sprawdź to na 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

starannością opracowania? – RamenChef

+0

Tak, na pewno System.out.println (x + "" + y); –

Powiązane problemy