Hello其他程序员。
也许这是一个微不足道的问题,但我无法找到解决方案,我也无法找到解决这个问题的任何线索。如果它的双重帖我很抱歉。
这是代码:!!不要忘记导入“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: [D@3590efa8
正如您所看到的,折扣后的价格是一团糟。它应该输出20号(25 * 0.8(Hell yea 20%折扣))。
这是第一个问题。
接下来是。我怎样才能确定哪个finalPrice会成倍增加?
谢谢大家。希望它对其他人也有帮助。
答案 0 :(得分:1)
你正在打印一个数组,而不是它的元素。
为了做到这一点,你必须遍历它们:
for (double d : prices) {
System.out.println(d);
}
修改强>:
阅读完评论后,您应该打印discount
变量:
System.out.println("Price after discount: " + discount);
答案 1 :(得分:0)
System.out.println("Price after discount: " + price);
Print Array of double------------------^
您需要迭代打印价格数组,如下所示
for (double d : price) {
System.out.println("Price is "+d);
}
答案 2 :(得分:0)
使用java.util.Arrays.toString(price)
获取可读结果。
你现在得到的是Object对象的默认toString方法:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}