在执行错误后如何让用户再次输入变量?

时间:2016-02-09 01:19:07

标签: java

twoElement = driver.cl.locate_element("two")
twoElement.click()

1 个答案:

答案 0 :(得分:1)

在我展示之前你可以让用户再次输入变量,我想指出你在else中再次声明yearEntered,这在Java中是非法的。只需删除int就可以解决问题。

现在,如果您输入错误输入的用户输入错误(即2015而不是1580),则可以执行以下操作:

Scanner scan = new Scanner(System.in);

System.out.println("Please enter a year on the Gregorian calendar. (After 1583)");


int yearEntered = 0; //initialize the variable with a default number
while (scan.hasNext()) { //keep scanning for the user input
    yearEntered = scan.nextInt(); //scan for the next int assuming the user only enters ints

    if (yearEntered < 1583) { //if the user entered less than 1583, then do another iteration till the user finally enters a number greater than or equal to 1582

        System.out.println("Please enter an integer greater than 1582");
        System.out.println("Try again");

    } else {
        break; //exits the loop
    }
}

//code for what you want to do with the correct yearEntered