有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在catch块中增加值?

我是编程新手。这是我的设想。我正在测试一个站点。如果某个链接未建立,则应转到捕捉块并移动到下一步。如果在k循环中失败,它应该执行k++;不是i++;或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
        }
    }
}

共 (1) 个答案

  1. # 1 楼答案

    在代码的顶部,有:

    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++;
    }