【发布时间】:2019-11-09 04:21:40
【问题描述】:
我写了一个 do-while 循环,但它并没有以某种方式运行 while 条件。 当我输入无效字符时,它应该回到开头并按预期重复。 我在 Visual Studio 上逐步运行代码,它表明代码甚至没有通过 while 条件。 (不管输入值是多少) 有人可以帮帮我吗? 非常感谢!
using System;
using static System.Console;
namespace a5
{
class Program
{
const string acceptedLetters = "EHLNTXZ";
static void Main(string[] args)
{
GetUserString(acceptedLetters);
ReadKey();
}
static string GetUserString(string letters)
{
string invalidCharacters;
do
{
invalidCharacters = null;
Write("Enter : ");
string inputCharacters = ReadLine();
foreach(char c in inputCharacters)
{
if(letters.IndexOf(char.ToUpper(c))==-1)
{
invalidCharacters = c.ToString();
}
}
if(invalidCharacters != null)
{
WriteLine("Enter a valid input");
}
return inputCharacters;
} while (invalidCharacters != null);
}
}
}
【问题讨论】:
-
你在循环结束时有一个
return,为什么你认为它会在循环结束后继续?
标签: c# loops while-loop do-while