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.