《Java實戰開發經典》第五章5.3

package xiti5;
public class Third {
    public static void main(String[] args) {
        T t=new T("want you to know one thing ");
          t.tell();
    }


}
class T{
    private String str;
    public T(String str){
        this.str=str;
    }

    public void tell(){
        char c[]=str.toCharArray();//將字符串分解爲字符數組
        int n=0;
        int m=0;
        for(int i=0;i<c.length;i++){
            if(c[i]=='n'){
                n++;
            }else 
                if(c[i]=='o'){
                    m++;
                }
        }
        System.out.println("該字符串中n出現了"+n+"次,o出現了"+m+"次");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章