简体   繁体   English

验证项目-程序不接受用户的首次输入

[英]Validation Project - Program doesn't accept first input from the user

Whenever it prompts the user to input a variable, the first entered variable does not get checked, but all variables afterwards get checked. 每当提示用户输入变量时,都不会检查第一个输入的变量,而是随后检查所有变量。

Here's my code: 这是我的代码:

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("^[!@#$%^&*].*")) {
               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();

    }

}

This is because you have the line 这是因为你有线

theVariable = scan.nextLine(); 

outside your while loop. 在while循环之外。 Remove that line and you should be golden. 删除该行,您应该会很高兴。

You are setting the variable value to the next scanned input before you do any checking. 在进行任何检查之前,您要将变量值设置为下一个扫描的输入。

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)");

//Here the variable is the value of the first input.

theVariable = scan.nextLine();
 //The variable has not been checked but right after the "while (true)" you scan for the next value.

while (true) {
 //Here you replace the first value with the second and never check it.     

 theVariable = scan.nextLine();
 //move this to the end of your lool, so the variable gets replace with the next one AFTER the checking code has run on the first one.

        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("^[!@#$%^&*].*")) {
       System.out.println("The variable is legal, but has bad style");
       System.out.println("Please enter a variable (or press 'q' to quit)");
       continue;
 //Move it to here, scanning the next variable after the first is checked.
   theVariable = scan.nextLine();
        }
   break;
 }

 System.out.println("The variable is legal and has good style");
scan.close();

    }

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM