Simple and straight forward approach is just convert all the input numbers into balls and sum it up. To display sum into overs, divide the totalBalls by 6 and takes the reminder as balls and display it like:
totalOvers.remainingBalls
Algorithm:
* Take the input numbers one by one
* For each input number:
-take the integer part and fraction part out
-take the addition of multiply by 6 into integer part and multiply by 10 into fraction part(actual input-integer part)
-add into totalBalls
* divide totalBalls by 6 to calculate the totalOvers and take the modulus 6 of it to calculate the remaining balls
* if reminder is greater than 0, then divide it by 10 and add into the totalOvers
Program:
--------------------------
import java.util.Scanner;
public class OversCalculator {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
double overs=0;
int totalBalls=0;
int temp=0;
while((overs=input.nextFloat())>0.0){
temp=(int)overs;//extract the complete overs
totalBalls=totalBalls+ temp*6+ (int)((overs-temp)*10);
}
overs=(totalBalls/6);
temp=totalBalls%6;
if(temp>0){
double remainingBalls=temp*(0.10);
overs=overs+remainingBalls;
System.out.println(overs);
}else{
System.out.println((int)overs);
}
}
}
Input-1:
----------------
1.2
7.5
5.5
0
Output-1:
----------------------
15
Input-2:
----------------
1.2
7.4
5.5
0
Output-2:
----------------------
14.5

0