• 07-11-2008, 05:57 AM
tamik0
Any help would be greatly appreciated, I need to display all of the amounts that are greater than the average amount(which is \$6.25). The section I am working on is at the bottom of the code(in bold).

public class PricesArray
{

public static void main(String[] args)
{

double[] prices = new double;

prices = 6.25;
prices = 7.85;
prices = 3.21;
prices = 3.31;
prices = 8.50;
prices = 9.37;
prices = 4.23;
prices = 8.37;
prices = 9.68;
prices = 1.99;
prices = 2.04;
prices = 3.66;
prices = 8.83;
prices = 4.91;
prices = 3.00;
prices = 7.84;
prices = 5.98;
prices = 2.09;
prices = 6.00;
prices = 7.00;

System.out.println("Sum of the prices Are:");

double sum = prices + prices + prices + prices + prices + prices + prices +
prices + prices + prices + prices + prices + prices + prices +
prices+ prices + prices + prices + prices + prices;

System.out.print("Sum is:" + sum);

System.out.print("The prices under \$5.00 are");

for(int i=0; i<prices.length; ++i)

{

if(prices[i]<5)

System.out.print(prices[i]+ " ");

}

System.out.print("The average price is");

double average;

sum = 0;

for(int i = 0; i < prices.length;i++)
{

sum += prices[i];

average = sum / prices.length;

System.out.println(prices[i]);

}

System.out.print("Prices higher than the average price are");

for(int i = 0; i < prices.length; ++i)

average = sum / prices.length;

{

if(average > prices[i])

System.out.println(prices[i] + " ");

}

}

}
• 07-11-2008, 06:25 AM
acp26b
Quote:

for(int i = 0; i < prices.length; ++i)

average = sum / prices.length;

{

if(average > prices[i])

System.out.println(prices[i] + " ");

}

To fix this problem, you just need to rearrange your code, see as follows.

Code:

```average = sum / prices.length; for(int i = 0; i < prices.length; ++i){     if(average > prices[i]){           System.out.println(prices[i] + " ");     } }```
• 07-11-2008, 10:41 AM
Niveditha
Yes using loops is the most easiest way to do it.