疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

计算拼音声调的java代码


 

以下,java培训机构为大家介绍一道计算拼音声调的java代码  

import java.io.*;//导入操作要用到的类

  public class main {

  public static void main(String args[]) {

  File file = new File("c:/test.txt");// 源文件位置

  FileReader fr;

  try {

  fr = new FileReader(file);

  BufferedReader in = new BufferedReader(fr);// 包装文件输入流,可整行读取

  String line;

  try {

  while ((line = in.readLine()) != null) {

  String a = pinyin(line);

  int shengdiaoLetter = Integer.parseInt(a.substring(

  a.length() - 1, a.length()));

  a = a.substring(0, a.length() - 1) + " "

  + a.substring(shengdiaoLetter, shengdiaoLetter + 1);

  System.out.println(a);

  }

  } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  } catch (FileNotFoundException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }// 创建文件输入流

  }

  private static String pinyin(String input) {

  int position = 0;

  int onlyaeiou = 0;

  input = input.toLowerCase();

  // 单个韵母不必说。 (单个的韵母,当然就标它上面了)

  if (input.contains("a")) {

  onlyaeiou++;

  }

  if (input.contains("e")) {

  onlyaeiou++;

  }

  if (input.contains("i")) {

  onlyaeiou++;

  }

  if (input.contains("o")) {

  onlyaeiou++;

  }

  if (input.contains("u")) {

  onlyaeiou++;

  }

  if (input.contains("v")) {

  onlyaeiou++;

  }

  if (onlyaeiou == 1) {

  if (input.contains("a")) {

  position = input.indexOf("a");

  }

  if (input.contains("e")) {

  position = input.indexOf("e");

  }

  if (input.contains("i")) {

  position = input.indexOf("i");

  }

  if (input.contains("o")) {

  position = input.indexOf("o");

  }

  if (input.contains("u")) {

  position = input.indexOf("u");

  }

  if (input.contains("v")) {

  position = input.indexOf("v");

  }

  return input + position;

  }

  if (input.contains("a")) {

  position = input.indexOf("a");

  return input + position;

  }

  // 没有a母找o e ,

  else if (input.contains("o")) {

  position = input.indexOf("o");

  return input + position;

  } else if (input.contains("e")) {

  position = input.indexOf("e");

  return input + position;

  }

  // iu 并列标在后,

  if (input.contains("iu")) {

  position = input.indexOf("iu") + 1;

  return input + position;

  }

  if (input.contains("a")) {

  position = input.indexOf("a");

  return input + position;

  }

  if (input.contains("e")) {

  position = input.indexOf("e");

  return input + position;

  }

  if (input.contains("i")) {

  position = input.indexOf("i");

  return input + position;

  }

  if (input.contains("o")) {

  position = input.indexOf("o");

  return input + position;

  }

  if (input.contains("u")) {

  position = input.indexOf("u");

  return input + position;

  }

  if (input.contains("v")) {

  position = input.indexOf("v");

  return input + position;

  }

  return input;

  }

  }