我在这里完全失败了...逻辑似乎设置正确但是while语句中的"响应"表示它在当前上下文中不存在.我在这里搜索,似乎在这种情况下似乎找到了相同的问题.问题是融合方法吗?
do
{
Console.WriteLine("enter a number between 1 and 5");
int x = Convert.ToInt32(Console.ReadLine());
Random r = new Random();
int rr = r.Next(1, 5);
Console.WriteLine("Do you want to continue? Please select yes or no.");
string response = Convert.ToString(Console.ReadLine());
} while (response == "yes");
Run Code Online (Sandbox Code Playgroud)
在一个范围内声明的变量(通常是一组大括号{ ... })在该范围之外是不可访问的.你已经response 在循环中声明了.你需要response在循环之外声明.
您还希望在比较之前修剪字符串中的空格,使用String.Trim().否则最后会有一个换行符(\n \n),导致您的比较失败.
string response;
do {
//...
response = Console.ReadLine().Trim();
} while (response == "yes");
Run Code Online (Sandbox Code Playgroud)