2014-03-28 50 views
0

我必須爲我的班級創建一個銷售新車和二手車的汽車經銷商的Java程序。我們需要一個可以詢問用戶銷售人員數量的程序。將他們的名字添加到一個String數組中。接下來,分別詢問新車和二手車的銷售總額。這是我的計劃到目前爲止。我想知道是否有辦法爲我的表格添加以下三個標題:名稱,已使用銷售額和新銷售額。二維數組來保存舊車和新車的銷售總額

public static void main(String[] args) { 
    Scanner in = new Scanner(System.in); 
    int numemployees; 
    System.out.println("Enter the Number of Employees: "); 
    numemployees = in.nextInt(); 
    String[] names = new String[numemployees]; 
    String line; 

    for (int i = 0; i < numemployees; i++) 
    { 
     System.out.print("Enter the name of the Salesperson: "); 
     names[i] = in.next(); 
    } 

    double [][] sales = new double [numemployees][2]; 
    for(int j = 0; j < numemployees; j++) 
    { 
     System.out.println("Enter New Car Sales: "+ names[j]); 
     sales[j][0] = in.nextDouble(); 
     System.out.println("Enter Used Car Sales: "); 
     sales[j][1] = in.nextDouble(); 

    } 

    for(int x = 0; x < numemployees; x++) 
    { 
     System.out.println(names[x] + "\t" + sales[x][0] + "\t" + sales[x][0] + "\t"); 
    } 


    } 
} 
+0

just use System.out.println(「Name」+「\ t」+「Used Sales」+「\ t」+「New Sales。」); before for(int x = 0; x

回答

1

打印頭使用的System.out.println:

System.out.println("Name \t Used Sales \t New Sales"); 
for(int x = 0; x < numemployees; x++) 
{ 
     System.out.println(names[x] + "\t" + sales[x][0] + "\t" + sales[x][1] + "\t"); 
} 

還有一想到要小心指數:您使用System.out.println(names[x] + "\t" + sales[x][0] + "\t" + sales[x][0] + "\t");。問題出在sales[x][0]它只會打印兩次相同的值。

+0

只有代碼纔會被查看。請給出你的答案的一些解釋。 – Kevin

+0

@Kevin更改:) –

0

1我建議使用String.format。然後你會得到一些很好的格式。

System.out.println(String.format("%-10s%-10s%-10s", "Name", "Used Sales", "New Sales")); 
for(int x = 0; x < numemployees; x++) 
{ 
    System.out.println(String.format("%8.2f%8.2f%8.2f", names[x], sales[x][0], sales[x][1])); 
}