在C#中,我想循环一个方法,只要该方法不返回一个整数。
public static int isAnInt()
{
rQuit = Console.ReadLine();
if (Int32.TryParse(rQuit, out isQuit) == false)
{
Console.WriteLine("Enter value again but as a number");
}
return isQuit;
}
/ 在主方法中,我想循环一个方法,只要该方法在C#中不返回一个整数。
while (int.TryParse(isAnInt() , out isQuit ) == false)
{
Console.WriteLine("This method is not an int try insert an int"); // For the method isAntInt I am trying to only
//loop the method as
// long as it is not an integer.
}
解决方案:
Console.WriteLine("Type something");
//It saves input
int thenumber;
//Check if Input is a number
while(!int.TryParse(Console.ReadLine(), out thenumber))
{//If input not a number then output this
Console.WriteLine("Not a number");
}
在这里,你去