2014-02-25 33 views
-1

你好同胞程序員。也許這是一個微不足道的問題,但我無法找出解決方案,我也找不到任何線索如何解決這個問題。如果它的雙重職位,我真的很抱歉。乘法後輸出顯示奇怪的字符,JAVA

這是代碼:!不要忘記導入你的「java.util.Scanner」!

double id[]=new double[4]; 
double price[]=new double[4]; 
String productName[]=new String[4]; 
id[0]=10001; 
id[1]=10002; 
id[2]=10003; 
id[3]=10004; 
price[0]=20; 
price[1]=25; 
price[2]=40; 
price[3]=10; 
productName[0]="T-Shirt"; 
productName[1]="More expensive T-Shirt"; 
productName[2]="Jacket"; 
productName[3]="Singlet"; 
int x=0; 
int z=0; 
int options; 
double discount; 

    System.out.println("Type in number of your choice between 1 to 4"); 
Scanner menu = new Scanner(System.in);   
options = menu.nextInt(); 

do { 
System.out.println("Chosen product n.: "+ options); 
}while (x > 0); 

switch (options){ 
    case 1: 
     System.out.println("Id of product: " + id[0] + " " + "Product price:" +price[0] + " " + "Name of product: " + productName[0]); 
     break; 
    case 2: 
     System.out.println("Id of product: " + id[1] + " " + "Product price: " +price[1] + " " + "Name of product: " + productName[1]); 
     break; 
    case 3: 
     System.out.println("Id of product: " + id[2] + " " + "Product price: " +price[2] + " " + "Name of product: " + productName[2]); 
     break; 
    case 4: 
     System.out.println("Id of product: " + id[3] + " " + "Product price: " +price[3] + " " + "Name of product: " + productName[3]); 
     break; 
       } 
discount=idPrice(price);  
    System.out.println("Price after discount: " + price); 
} 
static double idPrice(double finalPrice[]) 
{ 
return (finalPrice[1]/0.8); 
} 
} 

這裏鍵入數字1後的輸出:

Type in number of your choice between 1 to 4 
1 
Chosen product n.: 1 
Id of product: 10001.0 Product price: 20.0 Name of product: T-Shirt 
Price after discount: [[email protected] 

正如你所看到的價格折扣後的是一個爛攤子。它應該輸出20(25 * 0.8(地獄是20%的折扣))。

這是第一個問題。

接下來的事情是。我怎樣才能讓最終的價格倍增?

謝謝大家。希望對其他人也有幫助。

+1

'''price'''將打印參考陣列,而不是元素。您應該遍歷price []中的項目。無論如何,這是一個非常糟糕的設計。您應該創建一個帶有變量id,price和productName的Product類,並將這些值存儲爲Product的實例。 – NeplatnyUdaj

+0

謝謝你的評論。想你想使用多維數組嗎?像產品[編號] [價格] [名稱]? – user299930

回答

1

你打印一個數組,而不是它的元素。

爲了做到這一點,你必須通過他們迭代:

for (double d : prices) { 
    System.out.println(d); 
} 

編輯: 閱讀註釋後,你應該打印discount變量:

System.out.println("Price after discount: " + discount); 
+0

如果您添加到我的代碼的輸出將是 20.0 25.0 40.0 10.0 ,而不是數* 0.8 – user299930

+0

檢查更新的答案。:) –

+0

我可以從你的源代碼中看到,您已經定義了'折扣變量 –

0
System.out.println("Price after discount: " + price); 
     Print Array of double------------------^ 

你需要重複打印的價格陣列像下面

 for (double d : price) { 
      System.out.println("Price is "+d); 
     } 
0

使用java.util.Arrays.toString(price)得到一個可讀的結果。 你現在越來越是Object對象的默認toString方法:

public String toString() { 
    return getClass().getName() + "@" + Integer.toHexString(hashCode()); 
}