Results 1 to 6 of 6
- 11-26-2010, 09:13 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 7
- Rep Power
- 0
Can someone take a quick look at this array/forloop. java program for me?
This program is suppose to take gas prices and find the average using arrays and stuff.
This one keeps telling me the average is 00.0 which it isnt.
I'm pretty sure I named something wrong or something, but Im not sure what.
import java.util.*;
public class AverageGasPrice
{
public static void displayHeader()
{
System.out.println("K");
System.out.println("ID ");
System.out.println("This program takes sevral gas prices and outputs their average");
System.out.println("November 23 2010");
}
public static int readPrices(double[] gasPrice)
{
int priceCount = 0;
double price = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("This program calculates and displays the average");
System.out.println("price of a regular gallon of gas.");
System.out.println("Enter the price of gas or -99 to Quit: ");
price = keyboard.nextDouble();
while(price != -99)
{
gasPrice[priceCount] = price;
priceCount++;
price = keyboard.nextDouble();
}
return priceCount;
}
public static double averagePrice(double[] gasPrice, int priceCount)
{
double sum = 0;
//Compute the sum
for(int i=0; i < priceCount; i++)
{
sum = (gasPrice[priceCount] + sum);
}
//Compute the average
double average = sum/priceCount;
return average;
}
public static void main(String[] args)
{
displayHeader();
double[] gasPrice = new double[25];
int priceCount = readPrices(gasPrice);
double average = averagePrice(gasPrice, priceCount);
System.out.print("\n\nThe average price per gallon of regular");
System.out.println(" gas is " + average);
}
}
-
You will need to learn to place debugging code in your program. For instance, I made these changes:
Java Code:public static double averagePrice(double[] gasPrice, int priceCount) { double sum = 0; System.out.println(java.util.Arrays.toString(gasPrice)); // *** added to check if the array is OK // Compute the sum for (int i = 0; i < priceCount; i++) { sum = (gasPrice[priceCount] + sum); System.out.println("sum = " + sum); // **** added to check if sum is getting any data } // Compute the average double average = sum / priceCount;
-
Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?
To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
- 11-27-2010, 12:28 AM #4
Member
- Join Date
- Nov 2010
- Posts
- 7
- Rep Power
- 0
Thank you very much! I figured it was something like that. You saying where the error was at, really helped me figure it out. The program works fine now.
Also, I will keep in mind your advice about formating, I'd seen others with code like that, but wasn't sure how to make it look like that on the site.
Again, thank you very very much
-
- 11-27-2010, 01:28 AM #6
Member
- Join Date
- Nov 2010
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 07:53 PM -
Require quick program revision please
By SF163 in forum New To JavaReplies: 2Last Post: 11-07-2009, 01:04 AM -
ForLoop statement problem
By MomenT in forum New To JavaReplies: 4Last Post: 10-01-2008, 02:01 PM -
Quick Job required in Java
By taxman in forum Jobs OfferedReplies: 0Last Post: 01-02-2008, 12:46 PM -
Help with ForLoop
By Albert in forum New To JavaReplies: 1Last Post: 07-05-2007, 07:24 AM
Bookmarks