如果用户不给我字符串作为输入,我可以做一个循环吗?

本文关键字:一个 循环 我可以 用户 字符串 输入 如果 | 更新日期: 2023-09-27 18:31:55

例如,如果我要求用户给我他的名字(字符串),他写数字,符号,或者如果他按ENTER,我想要一个循环告诉他写字符串以便继续。我为整数做了一个循环,但我不知道如何制作字符串

Console.Write("Please enter the name of the student: ");
//here I made the input to turn into Capitals
name = Console.ReadLine().ToUpper(); 
Console.Write("Please enter their student number: ");
// here I set a condition order to continue. First the input must be integer and second it must be positive
while (!int.TryParse(Console.ReadLine(), out id)||id<0)
{
    Console.ForegroundColor = ConsoleColor.Red;
    Console.Write("The value must be of integer type, try again: ");
    Console.ResetColor();
}

如果用户不给我字符串作为输入,我可以做一个循环吗?

Console.Write("Please enter the name of the student: ");
//here I made the input to turn into Capitals
name = Console.ReadLine().ToUpper(); 
if (Regex.IsMatch(name, @"^[a-zA-Z]+$")) { // If letters only
 // Do something
}else{
 Console.WriteLine("Name must contain letters only");
}
Console.Write("Please enter their student number: ");
// here I set a condition order to continue. First the input must be integer and second it must be positive
while (!int.TryParse(Console.ReadLine(), out id)||id<0)
{
    Console.ForegroundColor = ConsoleColor.Red;
    Console.Write("The value must be of integer type, try again: ");
    Console.ResetColor();
}

这应该只检查字母。