面试题:
1.计算字符串"23743298"奇数位的和、偶数位的和
public class demo1 {
public static void main(String[] args) {
String s = "23743298";
int s1 = 0;
int s2 = 0;
for(int i=0; i < s.length(); i++) {
if(i%2 == 0) {
s1 += Integer.parseInt(s.substring(i, i+1));
}else {
s2 += Integer.parseInt(s.substring(i, i+1));
}
}
System.out.println(s+"奇数位的和是:"+s1);
System.out.println(s+"偶数位的和是:"+s2);
}
}
2.用代码统计"helloworld"中字母"o"的个数
public class demo2 {
public static void main(String[] args) {
String s = "helloworld";
//字符串转字符
char[] s1 = s.toCharArray();
int s2 = 0;
for(int i=0; i < s1.length; i++) {
if(s1[i] == 'o') {
s2 += 1;
}
}
System.out.println(s+"中字母'o'的个数是:"+s2);
}
}
浙公网安备 33010602011771号