Domanda

Hello fellow programmers.

Maybe it's a trivial question, yet I cant figure out solution, neither I can find any clue how to solve this. If its double post I am really sorry.

Here's the code: !! Don't forget to import your "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);
}
}

Here's the output after typing number 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

As you can see the Price after discount is a mess. It should output number 20(25*0.8 (Hell yea 20% discount)).

That's the first question.

Next thing is. How can I spicify which finalPrice will be multiplied?

Thank you all. Hope it will be helpfull to someone else also.

È stato utile?

Soluzione

You're printing an array, not it's elements.

In order to do so, you have to iterate through them:

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

Edit: After reading the comments, you should print the discount variable:

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

Altri suggerimenti

System.out.println("Price after discount: " + price);
         Print Array of double------------------^

You need to iterate to print the price array like below

        for (double d : price) {
            System.out.println("Price is "+d);
        }

Use java.util.Arrays.toString(price) to get a readable result. What you are getting now is the default toString method of the Object object:

 public String toString() {
     return getClass().getName() + "@" + Integer.toHexString(hashCode());
 }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top