我试图从未排序的列表中找到连续的值。实验代码如下:
num = [8, 9, 4, 1, 2, 3]
#(num[0+1]) next value
for i in range(len(num)-1): # not using -1 will cause index error
if num[i]+1==num[i+1]:
print('Con',num[i])
Run Code Online (Sandbox Code Playgroud)
问题:我无法使用当前代码获取最后一个值。我的输出不包括最后一个值。这是我得到的(9 号或 3 号):
Con 8
Con 1
Con 2
Run Code Online (Sandbox Code Playgroud)
我见过一些复杂的解决方案,对我来说有点难以理解。是否可以稍微调整 for 循环部分并获得整个序列?多谢。
在用户输入中使用 getter 和 setter 是一种好的做法吗?我寻找了一些例子,但没有找到。虽然下面的代码可以工作,但是这是一个好的做法吗?我注意到如果我使用下面的代码我将无法使用构造函数。谢谢你的解释。
public class Tests{
private String name;
private int id;
Scanner userinput = new Scanner(System.in);
public void setName(){
System.out.println("Enter a name: ");
name= userinput.next();
}
public String getName(){
return name;
}
public void displayInfo(){
setName();
System.out.println("You entered " + this.getName());
}
public static void main(String [] args){
Tests test = new Tests();
test.displayInfo();
}
}
Run Code Online (Sandbox Code Playgroud) 我的json文件看起来像这样[实际上它有更多,例如我只是放了2个块]
[{
"answerValue": "2021-02-01",
"parentId": "Policy",
"instance": 1,
"fieldId": "PolicyEffectiveDate"
},
{
"answerValue": "2012",
"parentId": "Insured",
"instance": 1,
"fieldId": "DateBusinessStarted"
}
]
Run Code Online (Sandbox Code Playgroud)
我想将它们存储在 HashMap 中并打印它们。
public void MapCheck() {
Map<String, Object> dataMap = new HashMap<>();
List<Map> lstMap = new ArrayList<>();
dataMap.put("answerValue:", "2021-02-01");
dataMap.put("parentId:", "Policy");
dataMap.put("instance:", 1);
dataMap.put("fieldId:", "PolicyEffectiveDate");
lstMap.add(dataMap);
dataMap.put("answerValue:", "Assurestart LLC");
dataMap.put("parentId:", "Insured");
dataMap.put("instance:", 1);
dataMap.put("fieldId:", "Business_Name");
lstMap.add(dataMap);
System.out.println(lstMap);
}
public static void main(String[] args) {
Test t = new Test();
t.MapCheck();
}
}
Run Code Online (Sandbox Code Playgroud)
预期:我想让它打印
[{parentId:=Policy, fieldId:=PolicyEffectiveDate, answerValue:=2021-02-01, instance:=1}, …Run Code Online (Sandbox Code Playgroud) 我读过一些在线文章,它们都指向 1 个方向,即当页面加载时找不到元素。在我的 setData() 中,您可以看到我尝试了一些操作,例如使用等待、隐式等待和第一次单击,然后发送用户名。然而似乎没有任何作用。我还考虑过在 pageProperties 中使用“wait”,但后来改变了主意,因为这可能是一个糟糕的设计。
错误 :
Exception in thread "main" org.openqa.selenium.InvalidElementStateException: invalid element state
Run Code Online (Sandbox Code Playgroud)
系统信息:Windows 10、ChromeDriver 2.37.544315、chrome=65.0.3
代码:
public class Tour {
public static WebDriver driver;
//browser URL information
public WebDriver getBrowser(String browser, String url){
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Downloads\\chromedriver.exe");
if(browser.equals("cc")){
driver= new ChromeDriver();
}
driver.get(url);
return driver;
}
// User name and continue button property from 1st page
public void pageUserNameProperty(String un){
WebElement login=driver.findElement(By.xpath("//input[@id='usernameOrEmail']"));
WebElement cont_btn=driver.findElement(By.xpath("//button[contains(@type,'submit')]"));
//WebDriverWait wait = new WebDriverWait(driver,30);
//wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='usernameOrEmail']")));
login.sendKeys(un);
cont_btn.click();
}
// Password and …Run Code Online (Sandbox Code Playgroud) 这是我使用Firebug检查元素时的样子
当我在xpath中尝试相同的语法时,它选择结果页面2.我在selenium IDE中尝试了相同的方法并单击了find,它在执行代码时选择结果页面2.我正在接受No Such Element异常
Xpath语法: //a[contains(@href,'/jobs?q=qa+engineer&l=Renton%2C+WA&start=10')]/span[contains(@class,'pn')][text()='2']
public void jobSearch(){
WebDriver driver= new FirefoxDriver();
driver.get("https://www.indeed.com");
driver.findElement(By.id("what")).sendKeys("QA Engineer");
driver.findElement(By.id("where")).clear();
driver.findElement(By.id("where")).sendKeys("Seattle,WA");
driver.findElement(By.id("fj")).click();
driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
driver.findElement(By.xpath("//a[contains(@href,'/jobs?q=qa+engineer&l=Renton%2C+WA&start=10')]/span[contains(@class,'pn')][text()='2']")).click();
Run Code Online (Sandbox Code Playgroud)
感谢您的宝贵时间和宝贵的建议.
我想写一个用户输入的程序,只要输入不是0就会继续要求输入.
我是怎么做到的:我决定检查第一个输入是否为0,如果为0则程序将立即退出.如果第一个输入不是0,那么它将要求用户输入更多数字.
我的问题:它只询问我2次,然后执行程序结束语句.
我的守则
import java.util.Scanner;
public class MyArList {
private static final int num2 = 0;
public static void main (String[] args){
Scanner userInput = new Scanner(System.in);
System.out.print("Enter 1st number: ");
int num1 = userInput.nextInt();
if (num1==0){
System.out.println("program exits");
}
else
{System.out.print("Enter more numbers: ");
while(!(userInput.nextInt()==0))
System.out.print("Progam ends ");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我也想过/尝试过,但那也不起作用
if( num1==0){
System.out.println("Program exits");
}
else{
do{
System.out.print("Enter more numbers: ");
int num2 = userInput2.nextInt();
}while(!(num2==0));
Run Code Online (Sandbox Code Playgroud)
感谢您宝贵的时间和意见.