StringBuilder
练习题
//输入一行字符 统计出数字字符的个数
Scanner sc = new Scanner(System.in);
char[] chars = sc.next().toCharArray();
int index =0;
for (int i = 0; i < chars.length; i++) {
if (chars[i] >= '0' && chars[i] <= '9') {
index++;
}
}
System.out.println(index);
}
代码实现
判断是不是回文数
Scanner sc = new Scanner(System.in);
String str = sc.next();
int left = 0, right = str.length() - 1;
boolean hui = true;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) hui = false;
left++;
right--;
}
System.out.println(hui ? "是回文" : "不是回文");
例题3:
这个思路 倒着循环 挨个判断 'a'^=32;可以让大写转成小写 小写转成大写