【问题标题】:ReadLine() closes my consoleReadLine() 关闭我的控制台
【发布时间】:2015-04-21 02:55:09
【问题描述】:

无论我做什么,我的第一个 Console.Readline 条目都会让我脱离控制台。无论我是在调试还是发布,都会发生这种情况。

我通过了我的 WriteLine

("Which Room Would you like to Trigger? \nRoom 1001 or Room 1002") 

一旦我点击 ReadLine,控制台就会关闭。

我仍在学习 C#,但我从未遇到过 Python raw_input(prompt) 的这个问题,我假设它等同于 C# ReadLine。

我的代码如下。

bool loopChk = true;

      do
      {
        Console.WriteLine("Which Room Would you like to Trigger? \nRoom 1001 or Room 1002");
        string line = Console.ReadLine();
        if (line == "1001")
        {
          await objClient.WritePropertyAsync(fqrs[0], "Present Value", a_bOn ? "on" : "off", CancellationToken.None);
          Console.WriteLine("You have Triggered Room 1001");
        }
        else if (line == "1002")
        {
          await objClient.WritePropertyAsync(fqrs[1], "Present Value", a_bOn ? "on" : "off", CancellationToken.None);
          Console.WriteLine("You have Triggered Room 1002");
        }
        else if (line == "exit")
        {
          break;
        }
      } while (loopChk);

【问题讨论】:

  • do-while 循环之后放置一个Console.ReadLine()
  • @jsve 在我输入一个值并按下回车键后没有做任何事情。
  • 这就是它应该做的。这不是你想要的吗?等到用户按下回车后退出程序?我想我不太明白你的问题。
  • @jvse 不,它应该留在 do while 循环中,直到我输入 exit。
  • hmm...您是否调试过line 的值以及代码从哪里开始?你似乎走在正确的轨道上。

标签: c# console readline console.writeline console.readline


【解决方案1】:

试试这个:

string line = null;

do {
    Console.WriteLine("Which Room Would you like to Trigger? \nRoom 1001 or Room 1002");
    line = Console.ReadLine();

    if (line == "1001")
    {
      await objClient.WritePropertyAsync(fqrs[0], "Present Value", a_bOn ? "on" : "off", CancellationToken.None);
      Console.WriteLine("You have Triggered Room 1001");
    }
    else if (line == "1002")
    {
      await objClient.WritePropertyAsync(fqrs[1], "Present Value", a_bOn ? "on" : "off", CancellationToken.None);
      Console.WriteLine("You have Triggered Room 1002");
    }
} while (line != "exit")

【讨论】:

  • 逐行遍历代码会发生什么?
  • 您也可以尝试注释掉await 这两行,看看是否有区别。
  • 当我单步执行代码时,Line 的值保持为空,一旦我输入一个值并按 Enter,控制台就会关闭。
  • 你注释掉了等待行吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-22
  • 1970-01-01
  • 2010-09-08
  • 2015-11-29
  • 2011-05-28
相关资源
最近更新 更多