I want to create a function that receives a number and check that it is within a range of 1 to 25. What is the best way to do this?
private int VerificaIntervalo(int numero)
{
if (numero > 25)
{
return 0;
}
return 1;
}
I want to create a function that receives a number and check that it is within a range of 1 to 25. What is the best way to do this?
private int VerificaIntervalo(int numero)
{
if (numero > 25)
{
return 0;
}
return 1;
}
It's much simpler than other options:
private bool VerificaIntervalo(int numero) {
return numero >= 1 && numero <= 25;
}
Ideally the method name could be more explanatory than this.
The simplest would be:
private bool VerificaIntervalo(int numero)
{
if(numero >= 1 && numero <= 25)
return true;
else
return false;
}
Here's an example for you to check if the numbers are in the range of 1 to 25, see below:
using System;
public class Test
{
public static void Main()
{
if (comparaNumero(90))
Console.WriteLine("Dentro do intervalo entre 1 a 25");
else
Console.WriteLine("Fora do intervalo");
}
public static bool comparaNumero(int n)
{
return (n >= 1 && n <= 25);
}
}
See worked on IdeOne .
private bool VerificaIntervalo(int numero)
{
if(numero >= 1 && numero <= 25)
{
return true;
}
else
{
return false;
}
}
Would that be?