I mean by that question, how do i check how often someone gave input in a: Convert.ToInt32(Console.ReadLine());
Im am beginner, so don't wonder (if its possible a dumb question :) I've already tried something like this: Thx for help! :)
int test1 = 0;
int input = Convert.ToInt32(Console.ReadLine());
if (input < test1) // I don't know how i can let check the computer, how many inputs the
user already did
{
// ACTION
}
CodePudding user response:
Another aproach will be to create another method which will encapsulate and add the functionality to keep and increase the counter. So even if you are calling Console.ReadLine()
from outside it is increasing the counter inside public YourMethodName()
.
public YourMethodName()
{
var inputCount = 0;
_consoleReadLine(ref inputCount);
var test1 = 0;
_consoleReadLine(ref inputCount);
_consoleReadLine(ref inputCount);
//inputCount will be 3 at this point
while (inputCount < test1)
{
//ACTION
//Notice that inputCount must increase or evaluate some condition
// to dont create an infinite loop
}
}
private string _consoleReadLine(ref int counter)
{
counter ;
return Convert.ToInt32(Console.ReadLine());
}
Notice: Convert.ToInt32(Console.ReadLine());
gives System.FormatException
when no numeric characters inserted.
while
loop will not be reachable because inputCount
will never be lower than test1
in this example, adjust variables.
If it does not solve your problem, please detail the question more deeply with more information to help us give you a better answer.
CodePudding user response:
You can just use another variable to keep track of this. For example:
var inputCount = 0;
var test1 = 0;
while(inputCount < test1)
{
var input = Convert.ToInt32(Console.ReadLine());
inputCount ;
}
This will increase inputCount
by 1 each time they input something in the console.