在Java的Character类中,有一个名为isJavaIdentifierStart(char ch)的方法。此方法确定 char 参数是否允许作为 Java 标识符中的第一个字符。当且仅当字符是 (1) 字母时,它才可以开始 Java 标识符; (2) 货币符号(即“$”); (3) 连接标点字符(即“_”,即下划线)。如果该字符可以开始 Java 标识符,则此方法返回 true;否则为假。
如何定义自己的 isJavaIdentifierStart 方法?到目前为止我已经:
public class MyCharacter {
public static boolean isJavaIdentifierStart(char c) {
return true;
}
}
//('a') == true);
//('7') == false);
//('_') == true);
//('$') == true);
//('Z') == true);
//('.') == false);
最佳答案
既然你开始用 switch/case/default
这样做:
public static boolean isJavaIdentifierStart(char c) {
switch (c) {
case 'a' : return true;
//Fill other cases
default: return false; //default is false
}
}
您还可以尝试一些 if else block :
public static boolean isJavaIdentifierStart(char c) {
if(c=='a'){
return true;
}else if(c=='_'){
return true;
}else if (){//Fill other cases
}else return false; //default is false
}
}
关于java - 定义 isJavaIdentifierStart 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20049642/