- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在下面的一些帮助将不胜感激。我正在编写一些控制台测试程序,并且希望能够从终端输入一些参数(我不想使用命令行参数-参数太多)。我已经尝试了一些变体,但是找不到如何完成此操作。以下是我的终端输入测试的最新版本。该程序的问题在于,如果遇到错误,完成程序将自动关闭,并且我想从Main()或fGetNumber()函数继续。虽然我可以看到为什么该程序无法正常工作,但它说明了我需要实现的目标-重新输入数字,但我找不到实现该目标的方法。如果输入有效数字,则没有问题。如果输入了无效的号码,我将找不到如何重新输入该号码的方法。
代码如下,并且我遇到的问题以“//////////”突出显示:
import "dart:async" as async;
import "dart:io";
void main() {
fGetNumber("Enter Nr of Iterations : ", 0, 999999)
.then((int iIters){
print ("In Main : Iterations selected = ${iIters}");
if (iIters == null) {
print ("In Main: Invalid Number of iterations : ${iIters}.");
} else {
fProcessData(iIters);
}
print ("Main Completed");
});
}
async.Future<int> fGetNumber(String sPrompt, int iMin, int iMax) {
print ("In fGetNumber");
int iIters = 0;
async.Completer<int> oCompleter = new async.Completer();
while (!oCompleter.isCompleted) { /////////// This loop does not work ///////
return fGetUserInput(sPrompt).then((String sIters) {
iIters = int.parse(sIters);
if (iIters < iMin || iIters > iMax) throw new Exception("Invalid");
oCompleter.complete(iIters);
return oCompleter.future;
}).catchError((_) => print ("Invalid - number must be from ${iMin} to ${iMax}")
).whenComplete(() => print ("fGetNumber - whenComplete"));// always gets here
}
print ("In fGetNumber (at end of function)"); //// it never gets here
}
async.Future<String> fGetUserInput(String sPrompt) {
print ("In fGetUserInput");
async.Completer<String> oCompleter = new async.Completer();
stdout.write(sPrompt);
async.Stream<String> oStream = stdin.transform(new StringDecoder());
async.StreamSubscription oSub;
oSub = oStream.listen((String sData) {
oCompleter.complete("$sData");
oSub.cancel();
});
return oCompleter.future;
}
void fProcessData(int iIters) {
print ("In fProcessData");
for (int iPos = 1; iPos <= iIters; iPos++ ) {
if (iPos%100 == 0) print ("Processed = ${iPos}");
}
print ("In fProcessData - completed ${iIters}");
}
最佳答案
// This loop does not work
// always gets here
whenComplete()
总是在成功或错误时被调用。
// it never gets here
fGetUserInput()
。收听
stdin
中的
fGetNumber
,仅在输入有效的情况下完成完成者/取消订阅:
async.Future<int> fGetNumber(String sPrompt, int iMin, int iMax) {
print ("In fGetNumber");
async.Completer<String> oCompleter = new async.Completer();
stdout.write(sPrompt);
async.Stream<String> oStream = stdin.transform(new StringDecoder());
async.StreamSubscription oSub;
oSub = oStream.listen((String sData) {
try {
int iIters = int.parse(sData);
if (iIters < iMin || iIters > iMax) throw new Exception("Invalid");
oCompleter.complete(iIters);
oSub.cancel();
} catch(e) {
print("Invalid - number must be from ${iMin} to ${iMax}");
stdout.write(sPrompt);
}
});
return oCompleter.future;
}
async.Future<int> fGetNumber(String sPrompt, int iMin, int iMax) {
print ("In fGetNumber");
async.Completer<int> oCompleter = new async.Completer();
fGetUserInput(sPrompt, oCompleter, (String sIters) {
try {
int iIters = int.parse(sIters);
if (iIters < iMin || iIters > iMax) throw new Exception("Invalid");
return iIters;
} catch(e) {
print ("Invalid - number must be from ${iMin} to ${iMax}");
stdout.write(sPrompt);
}
return null;
});
return oCompleter.future;
}
void fGetUserInput(String sPrompt, async.Completer oCompleter, dynamic inputValidator(String sData)) {
print ("In fGetUserInput");
stdout.write(sPrompt);
async.Stream<String> oStream = stdin.transform(new StringDecoder());
async.StreamSubscription oSub;
oSub = oStream.listen((String sData) {
var d = inputValidator(sData);
if(d != null) {
oCompleter.complete(d);
oSub.cancel();
}
});
}
Completer
只能完成一次。无论您编写什么代码,都无法循环反复完成它。
关于dart - async.Future async.Completer-如果发生错误,如何 “continue”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16438823/
编码新手,无法弄清楚为什么 continue 在我的 for 循环中不起作用。在打印语句之后,我想从顶部重新开始循环。如果有人能让我知道我做错了什么,那就太好了。我不允许共享 cse231_rando
我正在尝试运行连续异步查询。在我的 Windows 盒子上,我通过双击 ignite.bat 文件启动了 Apache Ignite,并尝试运行以下代码 - Data Publisher 客户端包点燃
目前我正在尝试了解 openCV 中连续矩阵和非连续矩阵之间的一些区别。有人向我建议连续矩阵提供更好的性能,因为程序不必在每一列的末尾跳回到下一行的开头。 简而言之,连续和非连续矩阵之间的可比较性能差
我有一个运行多个行的 cron,删除“坏”的行(根据我的标准)。我只是想知道优化脚本的最佳方法是什么。我可以执行以下操作之一: 让同一个 cron 在发现“坏”行后立即删除它们。 让相同的 cron
我已经在我的 Tfs2010 实例上运行了一个 CI 构建,但我还想运行一个门控检入构建。然而,我希望这两个构建运行的方式之间存在细微的差异。具体来说,我希望 Gated checkin 在失败时不产
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题?通过 editing this post 添加详细信息并澄清问题. 1年前关闭。 Improve this
Here和 here据说Continuation Monad解决了回调 hell 。 RX 和 FRP 也解决了回调 hell 。 如果所有这三个工具都解决了回调 hell ,那么就会出现以下问题:
我的 Java 缩进风格可能有点特殊,我经常这样写: object.myMethod( myFirstArgument, mySecondArgument, myThirdArgument,
关闭。这个问题需要details or clarity .它目前不接受答案。 想要改进这个问题吗? 通过 editing this post 添加详细信息并澄清问题. 关闭 2 年前。 Improve
我正在使用苹果的 SpriteKit 和 Swift 制作一个简单的游戏,并且遇到了问题。我试图让桨节点(paddle)围绕圆内的固定节点(anchorNode)连续旋转;但是,我无法弄清楚如何使桨节
我正在尝试为控制系统编写一个自适应 Controller ,即使用 Q-learning 的电源管理系统。我最近为车杆系统实现了一个玩具强化学习问题,并根据 Andrew NG 的笔记制定了直升机控制
我想知道一种原生支持定界延续的编程语言。我确实知道 Scala 曾经有 shift 和 reset,但这些都被删除了;而且我也知道 Seaside 似乎有类似的东西,但 Seaside 是一个图书馆,
在本教程中,您将通过示例了解 continue 语句。 continue 语句用于跳过循环的当前迭代,程序的控制流转到下一个迭代。 continue 语句的语法是: continue
背景 目前,我在本地构建解决方案时运行预构建命令。但是,在我的 VSTS 持续集成服务器上构建时不需要此命令。 问题 有没有办法在本地机器上构建时只运行预构建事件命令? 我知道您可以添加如下条件语句。
我有时会有 1 或 2 个测试在 CI 中失败,并且重新运行构建会导致它们通过。 如何自动重新运行这些不稳定的测试,以便我的构建第一次通过?有没有类似 mocha 的东西的this.retries ?
希望有人可以帮助我解决这个问题。让 TeamCity 启动并运行并在各种项目上进行构建。我希望能够自动将成功的 TeamCity 运行构建复制/部署到测试服务器。 我正在考虑使用 PowerShell
我希望使用持续集成,但我对 Ant、Jenkins、CruiseControl、Phing、PHPUnderControl 等并不了解…… 什么是真正的持续集成? 我正在使用 Zend Framewo
我已将 travis 设置为使用 tox(Python 2.7 和 3.5)进行测试并部署到 pypi。 Travis 尝试为每次测试运行部署包,而 pypi 正确地拒绝了第二次尝试。 我希望 tra
我正在尝试使用 pem key 将一些文件 scp 到不同的计算机,所以我不需要我的密码。但是,每次我尝试连接到一个新盒子时,它都会问我“说真的,你想连接吗?你想保存 key 吗?......”这太烦
使用dvc无疑有很多优势,它可以像集中式vcs一样使用,但是具有本地提交功能并且能够很容易地将项目 fork 给一些较小的团队,从而使得更难于支持持续集成吗?它有助于开发,使每个人都可以访问由CI服务
我是一名优秀的程序员,十分优秀!