2014-02-25 37 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()); 
}