gpt4 book ai didi

java - Java中控制键盘输入

转载 作者:行者123 更新时间:2023-12-01 13:10:06 25 4
gpt4 key购买 nike

我要编写这个程序,但不知道如何控制键盘输入:

Write a method to check whether the word entered is valid or not. Valid word should:

  1. Have at least 10 characters
  2. Start with a letter
  3. Contain a letter in upper case
  4. Contain at least 3 digits
  5. Contain a special character (e.g. @,$.% …etc)
  6. Contain a space

最佳答案

您不需要在每次击键时验证它,等到用户输入单词,然后验证。假设您使用控制台输入:

System.out.print("Enter something > ");
Scanner input = new Scanner(System.in);

String inputString = input.nextLine();

//perform validations on inputString, heres the first one:
//regex could be used instead of multiple if statements
if(inputString.length() < 10) {
System.out.println("Validation failed, word was too short");
}
else if ...

关于java - Java中控制键盘输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22947872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com