我需要的是一种方法,可以返回到该方法的顶部,这是触发了特殊事件,这是我在C#中的代码
public void SendRequest()
{
send:
{
var response = new RequestBuilder("https://google.com").
WithMethod(HttpMethod.GET).
Send();
if (response.Contains("Special Keyword"))
goto send;
// Continue..
}
}
此代码在C#中有效,它将重新发送请求,直到页面不包含“特殊关键字”为止
关于如何在Java中执行相同操作的任何想法?
这是我尝试过的方法,但是即使页面包含“特殊关键字”,它最终也会显示“完成”
public void SendRequest() {
send: {
String response = new RequestBuilder().
to("https://google.com").
buildAndSend().
toString();
if(response.contains("Special Keyword"))
break send;
// Continue..
System.out.println("Done");
}
}
答案 0 :(得分:3)
带有循环:
while (true) {
String response = ...;
if (response.contains("Special Keyword")) {
continue; // takes you back to the start of the loop.
}
break;
}
老实说,如果我看到了真实的代码,我会挠头。反转条件会更惯用:
while (true) {
String response = ...;
if (!response.contains("Special Keyword")) {
break;
}
// No continue.
}
答案 1 :(得分:2)
您可以使用while
循环。在您的情况下,do-while循环似乎很合适:
String response;
do {
response = new RequestBuilder().
to("https://google.com").
buildAndSend().
toString();
} while (response.contains("Special Keyword"));
System.out.println("Done");
答案 2 :(得分:0)
goto是Java中的保留作品,但未使用。
事实上,goto实在太糟糕了Why is goto Bad
答案 3 :(得分:0)
函数调用是替换goto的一种方法。因此,您可以像这样对函数进行递归调用(但将其转换为Java):
f