-4
import java.util.Scanner;
public class UserInput {
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean alpha = false;
boolean numeric = false;
boolean accepted = true;
boolean underscore=false;
Scanner s = new Scanner(System.in);
System.out.println("please Enter an Idintifire: " +s);
char c = s.next().trim().charAt(0);
if (Character.isDigit(c))
{
numeric = true;
} else if (Character.isLetter(c))
{
alpha = true;
}
else if (Character.isUnicodeIdentifierPart(c))
{
underscore = true;
}
else
{
accepted = false;
}
if (accepted && alpha && numeric && underscore)
{
System.out.println("this is an idintifire " +c);
}
else {
System.out.println(c+ " this not an idintifire ");
}
s.close();
}
}
please Enter an Idintifire: java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]
我希望用户只输入一个单个字母数字字;例如,(ab_23)不是(ab 23)
有什么建议吗?
我改变了它,但是当我进入(ab_23)的程序没有检查输入,它只是检查的第一个字符,有什么办法创建一个for循环来读取所有输入并将其与其他条件进行比较。 – user3368970
它仅检查第一个字符,因为您只在if else构造之前选择了0处的字符。检查我的答案的编辑部分。 – Adarsh