调用的方法不打印

时间:2013-06-13 03:20:53

标签: java printing char

这是我的代码:

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;
    }
}

当我编译并运行它时,没有任何打印。有人可以帮我弄清楚出了什么问题吗?

1 个答案:

答案 0 :(得分: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