2015-11-13 30 views
-4

什麼程序要我代碼:我如何正確編程?

代碼的可執行程序,它會產生 爲客戶在商店訂購的產品的一些 發票。右邊顯示了 程序的示例運行。

你的程序 必須要求的產品數量(最多 最大的12個產品可訂購)和 然後先後索要產品名稱和 其成本。所產生的發票包括:

商店的標題(如圖所示), 產品名稱和它們的成本, 所有產品計算成本, 計算5%的營業稅, 整體總成本 謝謝。

產品及其成本必須保存在 平行陣列中。兩種方法必須進行編碼。 一種方法將顯示標題。第二個 方法將接受所有 產品的計算成本並返回計算的銷售稅。 計算銷售稅的方法必須爲 對5%的稅率使用命名常數。

畫面例子應該是什麼樣子運行:http://imgur.com/F3XDjau

目前我的計劃是這樣的,到目前爲止,但林不知道,如果它是正確的,或者如果我需要的變量到一個數組。

public static void main(String[] args) { 
     Scanner input= new Scanner(System.in); 
     int product; 
     String products; 
     double cost; 

     System.out.println("How many products? "); 
     product=input.nextInt(); 

     for(int i = 0; i < product; i++){ 

      System.out.println("Product Name: "); 
      products=input.next(); 

      System.out.println("Cost: "); 
      cost=input.nextDouble(); 
     } 

     } 
    } 

回答

0

這是你可以填寫你的數組:

double[] costArray = new double[product]; 
for(int i = 0; i < product; i++){ 
    costArray[i] = input.nextDouble(); 
} 
0

你需要使用一個變量數組產品成本這樣的:

static final float TAXES = 0.05f; 

public static void main(String[] args) { 
    double sum = 0.0; 
    double tax; 
    Scanner input = new Scanner(System.in); 
    int product; 
    String products[]; 
    double cost[]; 

    System.out.println("How many products? "); 
    product = input.nextInt(); 
    products = new String[product]; 
    cost = new double[product]; 

    for (int i = 0; i < product; i++) { 

     System.out.println("Product Name: "); 
     products[i] = input.next(); 

     System.out.println("Cost: "); 
     cost[i] = Double.parseDouble(input.next().trim().replace(',', '.')); 
    } 

    indentedText(); 

    for (int i = 0; i < product; i++) { 
     System.out.printf(products[i] + '\t' + "%.2f %n", cost[i]); 
     sum = sum + cost[i]; 
    } 
    tax = calculateTaxes(sum); 
    System.out.printf("Sub total:" + '\t' + "%.2f %n", sum); 
    System.out.printf("Sales tax:" + '\t' + "%.2f %n", tax); 
    System.out.printf("Total to be paid:" + '\t' + "%.2f %n %n", (sum + tax)); 
    System.out.print('\t' + "Thank you!"); 

} 

private static void indentedText() { 
    System.out.print('\t' + "The Company Store" + '\n' + '\n'); 

} 

private static double calculateTaxes(double sum) { 
    return sum * TAXES; 
} 
+0

我會怎樣做這部分?店鋪名稱(如圖所示)產品名稱及其成本所有產品的計算成本計算的5%銷售稅總體總成本謝謝。 產品及其成本必須保持平行排列。兩種方法必須進行編碼。一種方法將顯示標題。第二種方法將接受所有產品的計算成本並返回計算的銷售稅。計算銷售稅的方法必須使用5%稅率的命名常數。 – Java

+0

@Java我已經編輯了我的最後一篇文章,解決你的問題。 – salembo