[英]How to check condition in a for loop and print the statement once?
I have some nested code and I want to check it's condition and print the statement once. 我有一些嵌套代码,我想检查它的条件并打印一次语句。 But I don't know how to reform the for loop or which is the ideal loop for this situstin to use.? 但是我不知道如何重新构造for循环,或者哪个是该西斯汀使用的理想循环。 I have written this and I am getting hello printed 4 times..I want it to be printed once.. Please help. 我已经写完了,现在已经打了4次打招呼了。我希望打一次。
countDev = dev.findElements(
By.xpath("html/body/form/div[2]/div[2]/ul/li")).size();
countProd = prod.findElements(
By.xpath("html/body/form/div[2]/div[2]/ul/li")).size();
System.out.println(countDev);
System.out.println(countProd);
if (countDev == countProd) {
for (int list = 1; list <= countDev; list++) {
if (dev.findElement(
By.xpath("/html/body/form/div[2]/div[2]/ul/li[" + list
+ "]/a/span"))
.getText()
.equals(prod.findElement(
By.xpath("/html/body/form/div[2]/div[2]/ul/li["
+ list + "]/a/span")).getText())) {
}
System.out.println("Hello!");
}
} else {
System.out.println("Bye, Bye!");
}
Not sure what you are trying to achieve, But just put a flag there and make it as true once print done. 不确定要达到的目标,但只需在其中放置一个标记,并在完成打印后将其设为true。
boolean printed =false;
for (int list = 1; list <= countDev; list++) {
if (your condtion here ) {
if(!printed ){
System.out.println("Hello!");
printed = true;
}
}
}
This loop will execute four times if countDev and countProd are equal to 4 如果countDev和countProd等于4,则此循环将执行四次
for (int list = 1; list <= countDev; list++)
Also, your if statement is empty and your 'Hello' print statement is outside the if but inside the loop 另外,您的if语句为空,而您的“ Hello”打印语句位于if之外,但在循环内
Hlo. 喂 Buddy..I can't understood you clearly... try this code.. 哥们..我不太清楚你的意思...尝试这段代码..
countDev = dev.findElements(
By.xpath("html/body/form/div[2]/div[2]/ul/li")).size();
countProd = prod.findElements(
By.xpath("html/body/form/div[2]/div[2]/ul/li")).size();
System.out.println(countDev);
System.out.println(countProd);
if (countDev == countProd) {
for (int list = 1; list <= countDev; list++) {
if (dev.findElement(
By.xpath("/html/body/form/div[2]/div[2]/ul/li[" + list
+ "]/a/span"))
.getText()
.equals(prod.findElement(
By.xpath("/html/body/form/div[2]/div[2]/ul/li["
+ list + "]/a/span")).getText())) {
System.out.println("Hello!");
break;
}
}
} else {
System.out.println("Bye, Bye!");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.