1. Member Join Date
Jul 2008
Posts
3
Rep Power
0

## Please Help, can't figure out what I'm doing wrong.

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] + " ");

}

}

}  Reply With Quote

2. Member Join Date
Jul 2008
Posts
8
Rep Power
0

## 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.

Java Code:
```average = sum / prices.length;
for(int i = 0; i < prices.length; ++i){

if(average > prices[i]){
System.out.println(prices[i] + " ");
}
}```  Reply With Quote

3. ## Yes using loops is the most easiest way to do it.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•