0

这是我的代码:

public class Exercixe09_04 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a string: ");
        String s1 = input.next();
        System.out.println("Enter a character: ");
        String s2 = input.next();

        char c = s2.charAt(0);

        int num = count(s1, c);

        System.out.print(num);
    }
    public static int count(String str, char a)
    {
        int count = 0;
        for(int i =0; i < str.length(); i++)
            if(str.charAt(i) == 'a'){
                count++;
            }
        return count;
    }
}

当我编译并运行它时,什么也没有打印出来。有人可以帮我找出问题所在吗?

4

1 回答 1

0
import java.util.Scanner;

public class Exercixe09_04 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String s1 = input.next();
        System.out.print("Enter a character: ");
        String s2 = input.next();

        char c = s2.charAt(0);

        int num = count(s1, c);

        System.out.println(num);
    }

    public static int count(String str, char a) {
        int count = 0;
        for (int i = 0; i < str.length(); i++)
            if (str.charAt(i) == a) {
                count++;
            }

        return count;
    }
}

样品运行:

falsetru@jmlee12:/tmp$ java Exercixe09_04
Enter a string: Hello
Enter a character: l
2
于 2013-06-13T03:34:57.490 回答