当要求输入一个合法字符串或者说一定格式的字符串时,就可以用java的解析方法。
比如想要一个[1, 1000]的正整数:
public static Integer mygetInt(String s) {
try {
Integer num = Integer.parseInt(s);
return (num <= 0 || num > 1000) ? -1 : num;
} catch (NumberFormatException e) {
return -1;
}
}
可以用抛出异常,判断输入是否符合要求。
访问String内的字符时,用.charAt(int index);方法
访问vector<String> str; 元素时,用str.elementAt(int index); 或 str.get(int index); 获取动态数组元素个数时用.size()方法
/by zzuwenjie 2017-3-28 14:29:20