1

I am creating a cricket score tracker android application as a project for an online course. In the application, I want to display the runs, wickets and the overs. There will be six buttons for user input: 1, 2, 3, 4, 6 and wicket.

When the user presses a button, the runs/wickets and the overs will update accordingly. But for displaying the overs, I am facing a problem.

In case you are unaware, in cricket, an over has 6 balls.

In my app, the game is limited to 5 overs(30 balls). So whenever a button is pressed, the over variable will increase by 0.1. I want the overs variable to automatically change to 2.0 when it reaches 1.6. When it reaches 2.6, it must change to 3.0 and so on until it reaches 5.0.

I don't know how to implement this in my app without using multiple if blocks. Here is my attempt:

if(overs<5.0){

    if(overs == 1.6){
    overs = 2.1;
    }

    if(overs == 2.6){
    overs == 3.1;
    }

    if(overs == 3.6){
    overs == 4.1;
    }

    else{
    overs += 0.1;
    }
}

I have to use this snippet of code inside every button method and that makes the whole thing look very ugly.

I am curious to know if there is any way to do this without using multiple nested if loops in java.

Senthamizh
  • 53
  • 1
  • 10

7 Answers7

2

Although I know nothing about cricket if your problem is using this code in every button, create this:

private double getOvers(double overs) {
    if(overs<5.0){

        if(overs == 1.6){
            overs = 2.1;
        }

        if(overs == 2.6){
            overs = 3.1;
        }

        if(overs == 3.6){
            overs = 4.1;
        }

        else{
            overs += 0.1;
        }
    }
    return overs;
}

and use it in every listener.

2

This may not be very elegant, but you could just add to the number of overs when a condition is met.

if (overs % 1 == .6) // decimal ends as .6
   overs += .5;

Since it is a float you are dealing with though, you may want to work with an epsilon value:

if (Math.abs((overs % 1)-.6) < .0000001)
    overs += .5;
else 
    overs += .1;
Nordii
  • 479
  • 1
  • 5
  • 15
1

Calling something like this and passing number of balls should work.

   public float calcOvers(float balls)
    {
            float overs;
            int overPrefix;

            int newBalls = balls * 10;
            overPrefix = (int)(newBalls / 6);
            balls = (newBalls % 6) / 10; 
            overs = overPrefix + balls;
            if(overs < 5)
            {
                return overs;
            }
            else
            {
                System.out.println("Exeeded 5 overs")
                return 0;
            }
    }
Nipun Thennakoon
  • 3,586
  • 1
  • 18
  • 26
0

Comparing float for identity is risky in all programming languages.
(Compare this discussion for admittedly not java: Is floating point math broken? ).

If you want to count in 10ths, use an int and display the counter divided by 10.

I.e.

int overs=0;

if(overs<50){

    if(overs == 16){
    overs = 21;
    }

    if(overs == 26){
    overs = 31;
    }

    if(overs == 36){
    overs = 41;
    }

    else{
    overs += 1;
    }
}

/* for anything which actually makes output use (1.0*overs)/10.0 */

Note that I also changed some == (comparison expression) into the obviously intended = (assignment statement).

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
0

I want the overs variable to automatically change to 2.0 when it reaches 1.6. When it reaches 2.6, it must change to 3.0 and so on until it reaches 5.0.

Assuming that you are coding in Java, why not use the ceil method?

 Math.ceil(overs);

For example:

double x = 7.5;
System.out.println("Math.ceil(x) : " + Math.ceil(x));

Result: 8.0

acarlstein
  • 1,799
  • 2
  • 13
  • 21
0

If you specially want to implement this code using float/Double, instead of using nested if statements you can simply get the decimal value and check if it is greater than or equal to 6 and increase the base value e.g //get the float decimal part which is actually the number of balls in the over

    double over = 4.6;
    //get the decimal value
    double ball = over % 1;
    // current value in ball is not a round off decimal value e.g 3.5 % 1 = .4999999
    //converting the ball value to a round off value as needed
    ball = Math.round(ball * 100.0) / 10.0;
    // ball = round(.60002*100)/10 
     if (ball >= 6){
       over = Math.floor(over) + 1;
    }
    System.out.println(over);
    //output => 5
abhishek kasana
  • 1,462
  • 12
  • 14
0

just get two variables

int overs=0;
int balls=0;

 private void OversCounter()
    {


         if (balls==6)
    {
        balls=0;
        overs+=1;
    }
Pingolin
  • 3,161
  • 6
  • 25
  • 40
Shahzeb
  • 41
  • 3