java - if语句中字符串扫描时出现 "String cannot be resolved to a variable"如何解决?

标签 java selenium-webdriver

最小可重现示例:

int value=scan.nextInt();

String S1=scan.nextInt();

if(value==2)
String S2=scan.nextLine();

//some lines of code

if(value==2)
driver.findElement(By.id("id2")).sendKeys(S2);

由于以下行,我收到“S2 无法解析为变量”:

driver.findElement(By.id("id2")).sendKeys(S2);

最佳答案

在此代码中

if(value==2)
    String S2=scan.nextLine();

变量S2立即超出范围。

尝试

String S2 = "";
if(value==2)
    S2=scan.nextLine();

关于java - if语句中字符串扫描时出现 "String cannot be resolved to a variable"如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57653141/

相关文章:

c# - Selenium - 查找具有标题的元素

java - 为什么我的 Java 单元测试结果将预期结果与内存位置进行比较?

java - 在 PaintComponent 之外使用 Graphics

java - 在android中存储大量图像

java - XSSFCell -> 如何从 long 中获取间隔时间值

html - 使用 Ruby 和 Selenium Webdriver find_elements 根据条件选择并单击特定链接

java - 是否可以在没有元素数量的情况下在 Java 中构造数组?

java - 我的 N-Queens 解决方案有什么问题?

selenium-webdriver - 单击链接打开一个新的浏览器窗口

java - 从 Excel 读取数据以提供测试用例