java限制只能输字符串

来源:百度知道 编辑:UC知道 时间:2024/07/02 07:11:48
比如 第一个字母要是A-Z的字母,后面的要求为a-z。

public class Test2 {
public static void main(String[] args) throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
char[] car = s.toCharArray();
if (judge(car)) {
System.out.println("输入合法");
}
}

public static boolean judge(char[] car) {
if ((int) car[0] < 65 || (int) car[0] > 90) {
System.out.println("首字母必须大写");
return false;
}
for (int i = 1; i < car.length; i++) {
if ((int) car[i] < 97 || (int) car[i] > 122) {
System.out.println("输入有误,必须输入a~z");
return false;
}
}
return true;
}
}
结果:
输入asdad
首字母必须大写
输入Asdfvsdg
输入合法
。。拷贝下来自己去测

public static boolean checkString(String source, String check)
{
for(int i = 0; i < source.length(); i++)