我是groovy的新手(在java上工作),尝试使用Spock框架编写一些测试用例.我需要使用"每个循环"将以下Java代码段转换为groovy代码段
List<String> myList = Arrays.asList("Hello", "World!", "How", "Are", "You");
for( String myObj : myList){
if(myObj==null) {
continue; // need to convert this part in groovy using each loop
}
System.out.println("My Object is "+ myObj);
}
Run Code Online (Sandbox Code Playgroud)
Groovy Snippet:
def myObj = ["Hello", "World!", "How", "Are", "You"]
myList.each{ myObj->
if(myObj==null){
//here I need to continue
}
println("My Object is " + myObj)
}
Run Code Online (Sandbox Code Playgroud) 我必须使用什么命令,才能离开for循环,也可以//code inside直接从跳转到//code after
//code before
for(var a in b)
{
switch(something)
{
case something:
{
//code inside
break;
}
}
}
//code after
Run Code Online (Sandbox Code Playgroud) 有时我在for循环中需要以下模式.有时在同一个循环中不止一次:
try:
var = 'attempt to do something that may fail on a number of levels'
except Exception, e:
log(e)
continue
Run Code Online (Sandbox Code Playgroud)
现在我没有看到一个很好的方法将它包装在一个函数中,因为它不能return continue:
def attempt(this):
try:
return this
except Exception, e:
log(e)
# 1. continue # <-- syntax error: continue not properly in loop or
# 2. return continue # <-- invalid syntax
# 3.
return False # <-- this sort of works, but makes me feel powerless
Run Code Online (Sandbox Code Playgroud)
如果我return False比我能:
var = attempt('to do …Run Code Online (Sandbox Code Playgroud) 我有一个脚本,它是发送电子邮件的其他链中的一个脚本.
在脚本开始时,我想检查文件是否存在,只有在存在时才继续,否则只是退出.
这是我的脚本的开始:
if [ ! -f /scripts/alert ];
then
echo "File not found!" && exit 0
else
continue
fi
Run Code Online (Sandbox Code Playgroud)
但是我不断收到一条消息说:
line 10: continue: only meaningful in a `for', `while', or `until' loop
Run Code Online (Sandbox Code Playgroud)
有什么指针吗?
我看到有人发布了以下答案来说明如果通过和继续之间的区别.我知道"a"列表是[0,1,2],我只是不知道"if not element"的结果是什么?为什么使用continue时,0不打印,只打印1和2?当a = 0时,"if not element"是"if not 0",它有特殊含义吗?
>>> a = [0, 1, 2]
>>> for element in a:
... if not element:
... pass
... print(element)
...
0
1
2
>>> for element in a:
... if not element:
... continue
... print(element)
...
1
2
Run Code Online (Sandbox Code Playgroud) 我有2个fors,嵌套后我有一些代码,如果条件在嵌套for中为真,我不想执行.如果我使用break代码将执行,所以(正如我在SCJP中学到的)我用于continue label;外部for.这是Java的弃用用法吗?老式的?有人建议使用递归或其他东西,但对我来说这是完全正常,简单,最新和完美的方式.
here:
for (bla bla) {
for (bla bla) {
if (whatever) continue here;
}
// some code I don't want to execute if whatever is true
}
Run Code Online (Sandbox Code Playgroud)
谢谢
编辑:
如果我将我的问题改为:你怎么能在多个嵌套的fors之间"导航"?这种方法是"推荐"方式吗?因为这就是它在SCJP Book中所说的.如果不是..这将意味着,Katherine Sierra和Bert Bates是错误的?
编辑2:
为什么continue label;气馁?我想要回答OOP或Java的概念或内部工作,可能会出错.
请问以下代码:
while True:
try:
print("waiting for 10 seconds...")
continue
print("never show this")
finally:
time.sleep(10)
Run Code Online (Sandbox Code Playgroud)
始终打印"等待10秒......"的消息,睡10秒钟,然后再做一次?换句话说,finally即使循环是continue-ed ,子句中的语句也会运行吗?
Form.ShowDialog()方法导致代码暂停,直到新调用的窗体关闭.我需要在调用ShowDialog()方法后继续运行代码.我用谷歌搜索并阅读有关使用backgroundworker的内容?但这是我第一次听说过,从未使用过它.
Form2 form2this = new Form2();
form2this.ShowDialog();
MessageBox.Show("Something");
Run Code Online (Sandbox Code Playgroud)
单击按钮后执行此代码,如何仍然调用ShowDialog以防止用户与主窗体交互,但仍然允许主窗体继续其工作?
很抱歉,如果已经讨论过,但我发现的一切似乎都很难执行这么简单的任务.我实际上很惊讶它不包含在SHowDialog方法中.例如ShowDialog().继续会很酷.
在最近使用ReSharper时,它建议我通过反转if条件和使用continue语句来减少某些地方的嵌套.
嵌套条件:
foreach(....)
{
if(SomeCondition)
{
//do some things
if(SomeOtherNestedCondition)
{
//do some further things
}
}
}
Run Code Online (Sandbox Code Playgroud)
继续陈述:
foreach(....)
{
if(!SomeCondition) continue;
//do some things
if(!SomeOtherNestedCondition) continue;
//do some further things
}
Run Code Online (Sandbox Code Playgroud)
我理解为什么你想减少嵌套的性能和内存问题以及两个片段如何相互等同的逻辑,但是从我的开发背景来看,前面的例子在阅读代码时更容易理解.
您更喜欢哪种方法?为什么?你continue在日常代码中使用嵌套ifs吗?
continue ×10
python ×3
c# ×2
for-loop ×2
if-statement ×2
bash ×1
deprecated ×1
each ×1
forms ×1
goto ×1
groovy ×1
java ×1
javascript ×1
loops ×1
nested-loops ×1
resharper ×1
spock ×1
try-finally ×1