昨天下午的笔试题,大概意思就是,输入一行字符串,要求统计字符串中数字,字母,空格,和其他字符的个数并输出;很简单的一道题,很快就写完了
import java.util.*;
public class Test01 {
public static void main(String[] args){
int num = 0;
int ch = 0;
int space = 0;
int other = 0;
Scanner in = new Scanner(System.in);
String str = in.nextLine();
int i =0;
while(i='0'){
num++;
}else if((temp>='a'&&temp<='z')||(temp>='A'&&temp<='z')){
ch++;
}else if(temp==' '){
space++;
}else{
other++;
}
i++;
}
System.out.println("数字:"+num+"个,字母:"+ch+"个,空格:"+space+"个,其他:"+other+"个");
}
}
但是提交上出通过测试用例为0%!然后我自测了一遍题目给的自测用例,发现是正确的,遂怀疑是输出格式有问题,代码逻辑应该没错,不然不应该自测用例通过提交却0%才对,于是检查了几遍格式,无果,就写下一题去了,后来提交了也没debug出来;
晚上复盘的时候,在自己的idea上仔细检查代码逻辑才发现,我把大写Z写成了小写z,直接GG
import java.util.*;
public class Test01 {
public static void main(String[] args){
int num = 0;
int ch = 0;
int space = 0;
int other = 0;
Scanner in = new Scanner(System.in);
String str = in.nextLine();
int i =0;
while(i='0'){
num++;
}else if((temp>='a'&&temp<='z')||(temp>='A'&&temp<='Z')){
ch++;
}else if(temp==' '){
space++;
}else{
other++;
}
i++;
}
System.out.println("数字:"+num+"个,字母:"+ch+"个,空格:"+space+"个,其他:"+other+"个");
}
}
改成这样,应该就是没问题的了,但是可惜是事后诸葛亮,估计我的笔试也凉了,唉,还是要心细才是



