制作它的最有效方法是什么,而不是每5秒打印一次,只有当getPercentage的返回值不同时,它才会在5秒检查时每次打印?
为了澄清我的问题,如果打印的字符串为50%,如果已经打印了50%,我希望这样做等待更改再次打印。
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class Wooot {
public static void main(String[] args) throws IOException, InterruptedException {
needData();
Report();
}
public static void Report() throws InterruptedException, IOException{
while (needData() == true)
{
Thread.sleep(5000);
System.out.println(getData("http://woot.com"));
}
}
public static boolean needData(){
return true;
}
public static String getData(String url) throws IOException {
Document doc = Jsoup.connect(url).timeout(0).get();
String percent = doc.select(".percent-remaining").first().text();
return percent;
}
}
答案 0 :(得分:1)
我认为你的意思是这样做:
String current = "";
String temp;
while (needData())
{
Thread.sleep(5000);
temp = getData("http://woot.com");
if (!current.equals(temp))
{
System.out.println(temp);
current = temp;
}
}