c# - 如何在 if 语句中使用 Console.ReadLine()?
问题描述
static void DelayText(string text, int delay = 1000) {
if (Console.ReadLine()) {
Console.WriteLine(text);
}
else {
System.Threading.Thread.Sleep(delay);
Console.WriteLine(text);
}
}
我正在尝试创建一种自动接受文本输入并将其延迟一定量的方法,但是如果按下某个键,它只会立即写入文本。
我知道错误,以及为什么会发生
main.cs(83,15): error CS0029: Cannot implicitly convert type `string' to `bool'
但我似乎无法弄清楚如何做到这一点。
有任何想法吗?
解决方案
您将在下面找到优化后的代码
static void DelayText(string text, int delay = 1000)
{
if (string.IsNullOrEmpty(Console.ReadLine()))
System.Threading.Thread.Sleep(delay);
Console.WriteLine(text);
}
推荐阅读
- react-native - 使用 React Native EXPO 分享到 Instagram 提要
- python - 如何从此元组中删除引号?我怎样才能使列表同质化
- arrays - Slurm 阵列采用双倍 CPU 比请求
- javascript - AWS Amplify 登录错误 - 登录错误... [TypeError: (0, _getRandomBase.default) 不是函数
- python - Pb 检索元组列表中的最大值
- excel - 检查重复项,然后复制重复项旁边的单元格
- jquery - 表中的 Ajax 表单在表的 ajax 重新加载后不提交
- reactjs - 反应日期范围选择器
- java - Android 服务未通知它已启动且 UI 间歇性锁定
- python - 在整个 pandas 数据帧上从零四舍五入