2019-04-17

//通过数组计算26字母随机出现100次,分别计算出各个字母出现的个数。

class Counter{

protected int value=0;

public void increment()

{

  value++;

}

public void discrement()

{

value--;

}

public void reset()

{

value=0;

}

public int petvalue()

{

return value;

}}

public class Sy4test3 {

public static void main(String[] args)

{

    int i,j;

    Counter counters[]=new Counter [26];

    for (j=0;j<26;j++)

    {

    counters[j]=new Counter ();

    }

    for (i=0;i<100;i++){

    int k=(int)(Math.random()*26);//就相当于随机出现的字母就已经赋值在每一个数组中了。

    counters[k].increment();

    }

    for(j=0;j<26;j++){

    char c=(char)('a'+j);

    System.out.println(c+"个数;"+counters[j].petvalue());


    }

}

}

你可能感兴趣的:(2019-04-17)