Hi, I'm currently trying to sort out a few problems with a program I'm trying to write. I'm pretty new to this, so the solution is probably going to be obvious, but here goes...
I have two seperate do-while loops (though they are very similar to each other) and which of the loops that is run depends on a boolean input from the user. The amount of times the loops is run depends on an int input from the user. Here's the problem I'm having - if I put the scanner for the int input (for the number of times the loop is run) inside the loop, then it works fine, however, the user keeps getting asked how many times the loops will be run over and over again for as many times as the loop is run, obviously because that code is inside the loop. If I put that code outside of the two loops, then the loops won't run more than once.
Here's the relevant code. I tried to edit it down because the program is quite long, but if I've left out anything important let me know.
boolean boolean1;
int innings = 0;
int innings2 = 0;
int numberofinnings;
Scanner sc;
sc = new Scanner(System.in);
System.out.println("Will player one be batting first? (true/false)");
boolean1 = sc.nextBoolean();
System.out.println("How many innings will each player have?");
numberofinnings = sc.nextInt();
if (boolean1==true)
{
do
{
//More code here
innings++;
}
while (innings < numberofinnings);
}
else if (boolean1==false)
{
do
{
//More code here
innings2++;
}
while (innings2 < numberofinnings);
}
I edited out the entire middle sections of the loops because they were very long I didn't think they were relevant, I'm pretty sure I didn't change anything in there between the loops working and the loops not working.
If I put the "How many innings will each player have?" part inside the two loops (once in both of them) then the loops work, but like I mentioned above, the code obviously keeps repeatedly asking how many times the loop will be run, because that part of the code is inside the loop. Why does it do this? How can I initialize the variable "numberofinnings" outside of the loops and then have them run the number of times that is entered into that variable?