你好同胞程序員。也許這是一個微不足道的問題,但我無法找出解決方案,我也找不到任何線索如何解決這個問題。如果它的雙重職位,我真的很抱歉。乘法後輸出顯示奇怪的字符,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%的折扣))。
這是第一個問題。
接下來的事情是。我怎樣才能讓最終的價格倍增?
謝謝大家。希望對其他人也有幫助。
'''price'''將打印參考陣列,而不是元素。您應該遍歷price []中的項目。無論如何,這是一個非常糟糕的設計。您應該創建一個帶有變量id,price和productName的Product類,並將這些值存儲爲Product的實例。 – NeplatnyUdaj
謝謝你的評論。想你想使用多維數組嗎?像產品[編號] [價格] [名稱]? – user299930