思路:关键就在于对字符串的每个字符的判别,if的判断过程要严谨,谨防漏掉,建议选用否定方式来判断,就是不符合标准直接判定为no跳出即可。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner a=new Scanner(System.in);
while(a.hasNext())
{
int n=a.nextInt();
a.nextLine(); //为了防止nextLine()的不输入
for(int i=0;i<n;i++)
{
String[] str=new String[n]; //创建字符串数组的输入,并且将之转化到char数组中去
boolean f=true;
str[i]=a.nextLine();
char[] cha=str[i].toCharArray();
if(!(cha[0]>='a' && cha[0]<='z' || cha[0]=='_'||cha[0]>='A'&& cha[0]<='Z')) //判断标志符首个字符的类型是否符合标准
{
System.out.println("no");
continue;
}
for(int j=1;j<cha.length;j++)
{
if(!(cha[j]>='a'&&cha[j]<='z'||cha[j]<='Z'&&cha[j]>='A'||cha[j]=='_'||cha[j]<='9'&&cha[j]>='0')) //判断剩余标志符是否符合标准
{
System.out.println("no");
f=false;
break;
}
}
if(f)
System.out.println("yes");
}
}
}
}
若有错误,还请指正。