如下两段程序
程序 1
package strTest;
import java.util.Scanner;
public class NumOfA {
public static void main(String[] args) {
// TODO Auto-generated method stub
while(true){
Scanner console = new Scanner(System.in);
System.out.println("请输入一个字符串");
String a = console.next();
String b = "a";
int num = 0 ;
for(int i = 0; i<a.length() ;i++){
char c = a.charAt(i);
if(b.equals(c)){
num++;
}
}
System.out.println("字符a出现的次数为" + num);
}
}
}
程序 2
public class Dog {
public static void main(String[] args) {
// TODO Auto-generated method stub
while(true){
Scanner console = new Scanner(System.in);
System.out.println("请输入一个字符串");
String a = console.next();
char b = 'a';
int num = 0 ;
for(int i = 0; i<a.length();i++){
char c = a.charAt(i);
if(b==c){
num++;
}
}
System.out.println("字符a出现的次数为" + num);
}
}
}
为什么第一段程序中得不到正确的结果?
望远
相关分类