《Java实战开发经典》第五章5.3

来源:互联网 发布:淘宝图片设计软件 编辑:程序博客网 时间:2024/05/16 15:12
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+"次");    }}
0 0