-1

私はパッセージまたは文字列の Flesch インデックスを計算し、理解するために必要な最低限の教育レベルを伝えるプログラムを書いています。

想定されているのは、人が計算したい文字列を入力すると、プログラムは単語数をカウントし、文の数をカウントし、各単語を調べて音節の数を計算することになっていますその単語を合計音節カウンターに追加します。音節を計算する方法は、単語内の隣接する母音の数を数えることです。たとえば、「Computer」の母音は o、u、e であるため、3 つの音節です。ただし、単語が文字 e で終わる場合、音節数の合計から 1 を引くと、「Passage」には a、a、および e がありますが、e は末尾にあるため、音節数は 2 になります。

これが私がこれまでに得たものです:

// Import scanner to get input from the user
import java.util.Scanner;

public class Flesch
{ 
  public static void main (String [] args)
  {
    public static final double BASE = 206.835;
    public static final double WORD_LENGTH_PENALTY = 84.6;
    public static final double SENTENCE_LENGTH_PENALTY = 1.015;

    Scanner input = new Scanner (System.in);

    // Declare variables for syllables and words
    int totalWords = 0;
    int totalSyllables = 0;

    // Prompt user for a passage of text
    System.out.println ("Please enter some text:");
    String passage = input.nextLine();

    // Words
    for (int i = 0; i < passage.length(); i++)
    {
      if (passage.charAt(i) == ' ') {
        totalWords++;
      }
      else if (passage.charAt(i) == '.') {
        totalWords++;
      }
      else if (passage.charAt(i) == ':') {
        totalWords++;
      }
      else if (passage.charAt(i) == ';') {
        totalWords++;
      }
      else if (passage.charAt(i) == '?') {
        totalWords++;
      }
      else if (passage.charAt(i) == '!') {
        totalWords++;
      }
    } 



    System.out.println ("There are " + totalWords + " words.");

    char syllableList [] = {'a', 'e', 'i', 'o', 'u', 'y'};

    // Syllables
    for (int k = 0; k < syllableList.length; k++)
    {
      for (int i = 0; i < passage.length(); i++)
      {
        if (passage.charAt(i) == syllableList[k]) {
          totalSyllables = totalSyllables + 1;
        } 
        if (lastChar(passage) == 'E' || lastChar(passage) == 'e') {
          totalSyllables = totalSyllables - 1;
        } else {
          break;
        }
      }
    }    
    System.out.println ("There are " + totalSyllables + " syllables.");
    input.close();

    public static char lastChar (String aWord)
    {
      char aChar = aWord.charAt(aWord.length() - 2);
      System.out.print (aChar);
      return aChar;
    }

    /** Return true if the last character in the parameter word is
      * a period, question mark, or exclaimation point, and false
      * otherwise
      **/
    public static boolean sentenceEnd (String word)
    {
      if (lastChar(passage) == '.') {
        return true;
      } else if (lastChar(passage) == '?') {
        return true;
      } else if (lastChar(passage) == '!') {
        return true;
      } else {
        return false;
      }
    }

    double fleschIndex = BASE - WORD_LENGTH_PENALTY * (totalSyllables / totalWords) - SENTENCE_LENGTH_PENALTY * (totalWords / totalSentences);

    if (fleschIndex > 100) {
      educationLevel = "4th grader";
    } else if (fleschIndex <= 100) {
      educationLevel = "5th grader";
    } else if (fleschIndex <= 90) {
      educationLevel = "6th grader";
    } else if (fleschIndex <= 80) {
      educationLevel = "7th grader";
    } else if (fleschIndex <= 70) {
      educationLevel = "8th grader";
    } else if (fleschIndex <= 60) {
      educationLevel = "9th grader";
    } else if (fleschIndex <= 50) {
      educationLevel = "high school graduate";
    } else if (fleschIndex <= 30) {
      educationLevel = "college graduate";
    } else if (fleschIndex < 0) {
      educationLevel = "law school graduate";
    }

    System.out.println ("The Flesch idex is " + fleschIndex + ".");
    System.out.println ("The text can be understood by a " + educationLevel + ".");
  }
} 

ブーリアン宣言の周りにセミコロンを置くようにという奇妙なエラーメッセージが表示され続けますが、これは意味がありません。

4

1 に答える 1