In: Computer Science
I'm working in Java and am working on a project where I need to find an average. The catch is that for some of the values there is no data because they will be entered at a later date. I have variables assigned so that for each entry if there is an input I'll have it say _____available = 1, otherwise the variable will equal 0. I'll use an example to make this more clear. Let's say I am trying to find the average time of a car in a race. The car is set to run 5 races. He has run races 1, 3, and 5 but not 2 or 5. Or maybe he has run all 5. It would change depending on the user's input. How can I write an equation in Java that will find the average, no matter how many races the racer has run? I was thinking I would use an if statement, maybe a series of them that say if race1Available = 1 then add the time for race 1 to total time, if not don't add it. And I would need to do that for all 5 races. I would also need to do that for the total number of races so that if I only run 3 races, then I would divide the total time by 3.
Update: pls see new code added at bottom. carAvg2.java
We don't need to have two separate variables to keep track
of
availability and time
we can only store Time, if its 0 means not available
as follows
Thanks
//------- carAvg.java
import java.util.Scanner;
public class carAvg {
public static void main (String args[]) {
int count = 0;
double total = 0;
double[] times = {0,0,0, 0,0};
Scanner sc = new Scanner(System.in); //to read input
System.out.println("Enter race time, otherwise enter 0 if race NOT run");
for (int i = 0; i < times.length ; i++)
{
System.out.print("enter Race " + (i+1) + ": ");
times[i] = sc.nextDouble();
if (times[i] > 0)
{
//increase count if race time entered
count++;
//also add time to the total
total = total + times[i];
}
}
System.out.println("Average time for " + count + " races= " + (total/count));
//Use following to display only 2 decimal places like 35.41 instead of 35.41456789
//String strAvg = String.format("%.2f", total/count);
//System.out.println("Average time for " + count + " races= " + strAvg);
}
}
//------- end of carAvg.java
Output
Added following code to Ask before entering race time
//------- carAvg2.java
import java.util.Scanner;
public class carAvg2 {
public static void main (String args[]) {
int count = 0;
double total = 0;
double[] times = {0,0,0, 0,0};
Scanner sc = new Scanner(System.in); //to read input
askNext:for (int i = 0; i < times.length ; i++)
{
do {
System.out.print("Do you want to run Race" + (i+1) + "? y/n: ");
String ch = sc.next();
if ( ch.equals("n") )
{
continue askNext; // askNext is label of for loop
}
else if ( ch.equals("y") )
{
break; //valid input so ask for time
}
} while(true);
do{
System.out.print("Enter time for Race" + (i+1) + ": ");
times[i] = sc.nextDouble();
if (times[i] >= 0)
{
//increase count if race time entered
count++;
//also add time to the total
total = total + times[i];
}
} while( times[i]<0 ); //repeat if -ve value entered
}
System.out.println("Average time for " + count + " races= " + (total/count));
//Use following to display only 2 decimal places like 35.41 instead of 35.41456789
//String strAvg = String.format("%.2f", total/count);
//System.out.println("Average time for " + count + " races= " + strAvg);
}
}
//------- end of carAvg2.java
new output