你好同胞程序员。也许这是一个微不足道的问题,但我无法找出解决方案,我也找不到任何线索如何解决这个问题。如果它的双重职位,我真的很抱歉。乘法后输出显示奇怪的字符,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