private String s;
private static int vowel;
private static int consonant;
Ques3_1(){
}
Ques3_1(String s){
this.s=s;
String change=s.toLowerCase();
int[] list = new int[s.length() - 1];
int counter = 1;
for(int i = 0; i < s.length()-1; i++){
for(int j = i + 1; j < s.length(); j++){
if(s.charAt(i) == s.charAt(j)){
counter = counter + 1;
}
}
list = counter;
if (s.charAt(i) != ' ')//{
//System.out.println(s.charAt(i) + " = " + list);
//}
counter = 1;
}
}
public String getS(){
return s;
}
public void setS(String s){
this.s=s;
}
public int getCountVowel(){
int vowel = 0;
String change=s.toLowerCase();
for(int i = 0; i < change.length(); i++ ){
if((change.charAt(i) =='a' ) || (change.charAt(i) =='e' ) || (change.charAt(i) =='i' ) ||
(change.charAt(i) =='o' ) || (change.charAt(i) =='u' && change.charAt(i) >=97 && change.charAt(i)<=122)){
vowel++;
}
}
return vowel++;
}
public int getCountConsonant(){
int vowel = 0;
int consonant =0;
String change=s.toLowerCase();
for(int i = 0; i < change.length(); i++ ){
if((change.charAt(i) =='a' ) || (change.charAt(i) =='e' ) || (change.charAt(i) =='i' ) ||
(change.charAt(i) =='o' ) || (change.charAt(i) =='u' && change.charAt(i) >=97 && change.charAt(i)<=122)){
vowel++;
}
else if(change.charAt(i) !=' ' && change.charAt(i) >=97 && change.charAt(i)<=122){
consonant++;
}
}
return consonant++;
}
public int getCountVowelConsonant(){
int vowel = 0;
int consonant =0;
String change=s.toLowerCase();
for(int i = 0; i < change.length(); i++ ){
if((change.charAt(i) =='a' ) || (change.charAt(i) =='e' ) || (change.charAt(i) =='i' ) ||
(change.charAt(i) =='o' ) || (change.charAt(i) =='u' && change.charAt(i) >=97 && change.charAt(i)<=122)){
vowel++;
}
else if(change.charAt(i) !=' ' && change.charAt(i) >=97 && change.charAt(i)<=122){
consonant++;
}
}
return (int)vowel+consonant;
}
}
//wong sit keng (147717)
import java.util.Scanner;
public class testQues3{
public static void main(String[]args){
Scanner input=new Scanner(System.in);
System.out.print("Please enter a string: ");
String str=input.nextLine();
Ques3_1 s= new Ques3_1(str);
do{
System.out.println("1) Count the number of vowels\n2) Count the number of consonants\n3) Count both vowels and consonants\n4) Enter another string\n5) Exit the program");
System.out.print("Your choose is: ");
int press=input.nextInt();
if(press==1)
{
//s. getCountVowel();
System.out.println("The number of vowel is "+s.getCountVowel());
System.out.println("");
}
else if(press==2)
{
//s.getCountConsonant();
System.out.println("The number of consonant is "+s.getCountConsonant());
System.out.println("");
}
else if(press==3)
{
s.getCountVowelConsonant();
System.out.println("The number of both vowel and consonant is "+s.getCountVowelConsonant());
System.out.println("");
}
else if(press==4)
{
System.out.print("Please enter another string: ");
str=input.nextLine();
}
else if(press==5)
{
System.out.println("Exit!");
System.exit(0);
}
else
{
System.out.println("No such command");
System.out.println("");
}
}while(str!="null");
}
}
***为么我不能 4.ENTER ANOTHER STRING