乘法后的输出显示奇怪的字符,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: [D@3590efa8

如您所见,打折后的价格一团糟。它应该输出数字20(25 * 0.8(是的,折​​扣20%))。

那是第一个问题。

接下来是。我如何具体化将要乘以的finalPrice?

谢谢你们。希望对其他人也有帮助。

康斯坦丁·约夫科夫(Konstantin Yovkov)

您正在打印一个数组,而不是它的元素。

为此,您必须遍历它们:

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

编辑:阅读评论后,您应该打印discount变量:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章