计算元音和单词的方法

时间:2015-10-26 05:24:45

标签: java string methods

我正在编写一个程序,需要两个基于用户选项的方法。出现提示时,用户选择是否要计算元音或单词。一旦他们选择了他们的选项,我需要打印他们的结果,并继续要求更多输入,直到他们输入“q”。其中一种方法输出一个单词中的元音数量,另一种方法输出一个单词中的单词数量,但它不起作用。我是编程新手并且有一个非常基本的理解,所以我对答案的选择是有限的。这是我到目前为止所做的:

*我修复了我的=与==签名问题。我尝试编译时出现的第一件事是char和java字符串之间不兼容的类型。我不知道如何告诉计算机寻找一封信作为开始其余程序的选项。

    import java.util.Scanner;
    public class MethodTester
    {
       public static void main (String[] args)
       { 
   Scanner in = new Scanner (System.in);
   System.out.println ("Please enter 'v' to count vowels or 'w' to count words.");
   String count=in.nextLine();
   char choice = count.charAt(0);
   boolean done = false;

   while (!done)
   {
   if (choice == "v")
   { 
       Sytem.out.println("Please enter a single word or press q to quit.");
       string str = in.nextLine();

       System.out.println(str+" has "+str.countVowels+"Vowels.");
       return countVowels;

    } else if (str == "q")
        {done = true;

        }  else if (choice == "w")
       { System.out.println("Please enter a sentence or phrase or press q to quit.");
         string str = in.nextLine(); 

         System.out.println(str+ " has "+ str.countWords+"words.");
         return countWords;



        } else if (str == "q")
        {done = true;
        }
        }
        System.out.println("You have quit.");  
}


       public static int countVowels (String str)
       {char c=str.charAt(0);
   int length = str.length();
   int count = 0;
   int location = str.charAt(0);

   do{
       if (c=="a" || c=="e" || c=="i" || c=="o" || c=="u" || c=="A" || c=="E" || c=="I" || c=="O" || c=="U" ){
        location++;
        count++;
        c++;
    }}
    while(location<length);
    return count;
}
public static int countWords (String str)
{
    int countWords=0;
    boolean word = false;
    int endStr = str.length() -1;

    for (int i=0; i < str.length(); i++)
    {
        if (Character.isLetter(str.charAt(i)) && i != endStr)
        {
            word = true;
        }
        else if (!Character.isLetter(str.charAt(i)) && word)
        {
            countWords++;
            word = false;
        }
        else if (Character.isLetter(str.charAt(i)) && i ==endStr)
        {
            countWords++;
        }
    }
    return countWords;
}   
    }

0 个答案:

没有答案
相关问题