2019-04-16

//通过类方法,计算出随机出现的字母的元音,半音节,辅音字母分别的次数。

class Counter{   //定义个用于计数的类

protected int value=0;

public void increment(){  

value++;

}

public void decrement(){

value--;

}

public void reset(){

value=0;

}

public int petvalue(){

return value;

}

}

public class Sy3test2 {

public static void main(String[] args) {

Counter counter1= new Counter();

Counter counter2= new Counter();

Counter counter3=new Counter ();

counter1.reset();

counter2.reset();

counter3.reset();

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

{

char c=((char)(Math.random()*26+'a'));

switch(c)

{case 'a':

case'e':

case'o':

case'u':

case'i':counter1.increment(); break;

case'y':

case'w':counter2.increment(); break;

default:counter3.increment();break;

}

System.out.print(c+" ");

}

System.out.println();

System.out.println("元音字母:"+counter1.petvalue());

System.out.println("半元音字母:"+counter2.petvalue());

System.out.println("辅音字母:"+counter3.petvalue());

}}

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