0

我正在编写一个程序来计算字符串中元音的数量,如果元音多于辅音,则返回 true。如果不是,则为假。这是一项家庭作业,但跑步者不是其中的一部分。我想测试一下我的程序是否有效,它应该(希望!)。

现在,对于我们所有的家庭作业和实验室,通常都会提供跑步者。我们从来没有被教过如何编写一个,这很糟糕,因为我想检查我的代码。我尝试模仿过去的跑步者,但我的跑步者不断出现错误,其中一些显示为:“找不到符号”如何为这个程序创建跑步者?

这是我的代码:

import static java.lang.System.*;
public class StringAnalyzer {
  //String word;
  public static boolean hasMoreVowelsThanConsonants(String word) {
    // String word = string.toUpperCase();
    int vowelCount;
    int newLength;
    for (vowelCount = 0; word.length() >= 1; vowelCount++) {

      if (word.indexOf("A") != 1) {
        vowelCount++;
      } else if (word.indexOf("E") != 1) {
        vowelCount++;
      } else if (word.indexOf("I") != 1) {
        vowelCount++;
      } else if (word.indexOf("O") != 1) {
        vowelCount++;
      } else if (word.indexOf("U") != 1) {
        vowelCount++;
      }


      newLength = (word.length() - vowelCount);

      if (vowelCount > newLength) {
        return true;
      } else {
        return false;
      }

    }
    return true;
  }
}

如果你发现任何问题,我总是除了建议:)

这是我的“跑步者”(它很糟糕,哈哈):

导入静态 java.lang.System.*;

import static java.lang.System.*;


public class StringAnalyzerRunnerCDRunner {
  public static void main(String[] args) {
    hasMoreVowelsThanConsonants("DOG");
  }
}

谢谢 :)

4

2 回答 2

0

I'm not really sure what you're question is, but since you're asking for advice on the code you've written, heres my 2c.

Some tweaks :

public class StringAnalyzer {

    public static void main(String[] args) {

        String word;

        word = "Dog";
        System.out.println(word + " has more vowels than consonants? " + hasMoreVowelsThanConsonants(word));

        word = "Ace";
        System.out.println(word + " has more vowels than consonants? " + hasMoreVowelsThanConsonants(word));
    }

    public static boolean hasMoreVowelsThanConsonants(String word) {
        int vowelCount = 0;
        int consonantCount = 0;

        String[] split = word.split("");
        for(String s : split){
            if(s.toUpperCase().matches("A|E|I|O|U")){
                vowelCount++;
            } else {
                consonantCount++;
            }
        }

        return vowelCount > consonantCount;
    }
}

Some points I've changed:

  1. Removed unused System import, its not required
  2. Added main method, which is your entry point, or "runner" as you refer to it.
  3. Introduced split word.split("");, this will give you an array of characters from the String, it's a bit nicer to work with.
  4. Convert each character to upper case, then compare it to a regex, meaning either A,E,I,O or U.
  5. Kept a count of consonants/vowels, the return just compares if vowels are greater, and returns true if so.

Theres many ways to do this. You might like to consider what happens if the counts are equal...or if the word is null, protect from null pointers etc, but that's down to you ;)

于 2015-02-25T04:45:47.763 回答
0

首先,您收到的错误消息error: cannot find symbol是:未导入打包的类,和/或您拼错了变量、类或方法名称

另一种方法:

您可以将主类命名为 from public class StringAnalyzer{topublic class Main {class StringAnalyzer

我不想偏离你所拥有的太远,但这里是另一种方法的片段:

import java.lang.*;
import java.util.Scanner;

class StringAnalyzer
{

   public static void main(String args[])
   {
      Scanner in = new Scanner(System.in);
      String sampleword = in.nextLine();
      System.out.println("You entered string: "+sampleword);
      System.out.println("Is your vowels more than the consonants: "+ hasMoreVowelsThanConsonants(sampleword));
   }

    private static boolean hasMoreVowelsThanConsonants(String word) {
        int vowelCount = 0;
        int newLength = 0;

        for (int i = 0; i < word.length(); i++) {
          if (word.charAt(i) == 'A') {
            vowelCount++;
          } else if (word.charAt(i) == 'E') {
            vowelCount++;
          } else if (word.charAt(i) == 'I') {
            vowelCount++;
          } else if (word.charAt(i) == 'O') {
            vowelCount++;
          } else if (word.charAt(i) == 'U') {
            vowelCount++;
          }
        }

        newLength = word.length() - vowelCount;

        return vowelCount > newLength;
      }
   }
}
于 2015-02-25T06:02:20.847 回答