如何增加 catch 块中的值?
How to increment the value in catch block?
我是编程新手。这是我的场景。我正在测试一个网站。如果没有找到 link ,它应该转到 catch 块并转到下一步。如果它在 k 循环中失败,它应该执行 k++;不是我++;或j++; .如果它在 j 循环中失败,它应该增加 j 值而不是 i 值或 k 值。怎么做?
public static void main(String[] args) throws NoSuchElementException {
int i = 0, j = 0, k = 0;
String links[] = new String[10];
links[0] = "link1";
links[1] = "link2";
links[2] = "link3";
links[3] = "link4";
WebDriver driver = new FirefoxDriver();
while (i < 4) {
try {
driver.get(links[i]);
driver.findElement(By.xpath("//div[contains(@id,'image')]")).click();
while (j < 5) {
driver.findElement(By.xpath("//div[contains(@id,'header')]")).click();
while (k < 8) {
driver.findElement(By.xpath("//div[contains(@id,'title')]")).click();
k++;
}
j++;
}
i++;
} catch (NoSuchElementException e) {
System.out.println(e);
// How to increment the value of i or j or k
}
}
}
在代码的顶部,有:
char currentLoop = 'i';
然后在每个 while 循环声明之后立即相应地设置值,如下所示:
while (i < 4) {
currentLoop = 'i';
...
while (j < 5) {
currentLoop = 'j';
...
while (k < 8) {
currentLoop = 'k';
...
}
}
}
然后在捕捉中做:
if (currentLoop == 'i') {
i++;
}else if (currentLoop == 'j') {
j++;
}else if (currentLoop == 'k') {
k++;
}
我是编程新手。这是我的场景。我正在测试一个网站。如果没有找到 link ,它应该转到 catch 块并转到下一步。如果它在 k 循环中失败,它应该执行 k++;不是我++;或j++; .如果它在 j 循环中失败,它应该增加 j 值而不是 i 值或 k 值。怎么做?
public static void main(String[] args) throws NoSuchElementException {
int i = 0, j = 0, k = 0;
String links[] = new String[10];
links[0] = "link1";
links[1] = "link2";
links[2] = "link3";
links[3] = "link4";
WebDriver driver = new FirefoxDriver();
while (i < 4) {
try {
driver.get(links[i]);
driver.findElement(By.xpath("//div[contains(@id,'image')]")).click();
while (j < 5) {
driver.findElement(By.xpath("//div[contains(@id,'header')]")).click();
while (k < 8) {
driver.findElement(By.xpath("//div[contains(@id,'title')]")).click();
k++;
}
j++;
}
i++;
} catch (NoSuchElementException e) {
System.out.println(e);
// How to increment the value of i or j or k
}
}
}
在代码的顶部,有:
char currentLoop = 'i';
然后在每个 while 循环声明之后立即相应地设置值,如下所示:
while (i < 4) {
currentLoop = 'i';
...
while (j < 5) {
currentLoop = 'j';
...
while (k < 8) {
currentLoop = 'k';
...
}
}
}
然后在捕捉中做:
if (currentLoop == 'i') {
i++;
}else if (currentLoop == 'j') {
j++;
}else if (currentLoop == 'k') {
k++;
}