用JAVA来判断C语言合法标识符,实在不知道错在哪里~~
帮一个朋友看错的地方,但找了好久怎么也找不出来~~自己写一个又是对的,郁闷ing
错误的代码如下,原题地址是:http://acm.hdu.edu.cn/showproblem.php?pid=2024
import java.util.*;
public class Main
{
public static void main(String[] args)
{
String buffer;
Ident a = new Ident();
int n, i;
Scanner keyboard = new Scanner(System.in);
n = keyboard.nextInt();
for (i = 0; i < n; i++)
{
Scanner read = new Scanner(System.in);
buffer = read.nextLine();
if (a.S2(buffer) && a.S1(buffer))
System.out.println("yes");
else
System.out.println("no");
}
}
}
class Ident
{
public Ident() { }
boolean S1(String str)
{
for (int i = 1; i < str.length(); i++)
{
if (!((str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')
|| (str.charAt(i) >= 'a' && str.charAt(i) <= 'z')
|| str.charAt(i) == '_'
|| (str.charAt(i) >= '0' && str.charAt(i) <= '9'))
)
return false;
}
return true;
}
boolean S2(String str)
{
if (
(str.charAt(0) >= 'a' && str.charAt(0) <= 'z')
|| (str.charAt(0) >= 'A' && str.charAt(0) <= 'Z')
|| str.charAt(0) == '_'
)
return true;
else
return false;
}
}