Всякий раз, когда он запрашивает у пользователя ввод переменной, первая введенная переменная не проверяется, но все переменные впоследствии проверяются.Проект проверки - Программа не принимает первый вход от пользователя
Вот мой код:
import java.util.*;
public class classOfValidation {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String theVariable = null;
System.out.println("This program checks the validity of variables");
System.out.println("Please enter a variable (or press 'q' to quit)");
theVariable = scan.nextLine();
while (true) {
theVariable = scan.nextLine();
if ("q".equals(theVariable)) {
System.out.println("Program quitted. Goodbye!");
continue;
}
if (theVariable.matches("^\\d+.*|.*\\s+.*")) {
System.out.println("The variable is illegal");
System.out.println("Please enter a variable (or press 'q' to quit)");
continue;
}
if (theVariable.matches("^[[email protected]#$%^&*].*")) {
System.out.println("The variable is legal, but has bad style");
System.out.println("Please enter a variable (or press 'q' to quit)");
continue;
}
break;
}
System.out.println("The variable is legal and has good style");
scan.close();
}
}
Вы вызываете 'theVariable = scan.nextLine();' перед циклом while и immidiatly снова вызываете его как первый оператор в цикле. Я бы предположил, что это означает, что ваш «не проверял». – SomeJavaGuy
Исправлено! Спасибо, должен удалить этот пост – TheNewYo
Возможный дубликат [Пропуск nextLine() после использования следующих(), nextInt() или других методов nextFoo()] (http://stackoverflow.com/questions/13102045/skipping-nextline-after -Использование-nextint следующего или-другой-nextfoo-метода) – Raf