###前提・実現したいこと
単語の複数形への変換は、次のルールに従い行われます。
・末尾が s, sh, ch, o, x のいずれかである英単語の末尾に es を付ける
・末尾が f, fe のいずれかである英単語の末尾の f, fe を除き、末尾に ves を付ける
・末尾の1文字が y で、末尾から2文字目が a, i, u, e, o のいずれでもない英単語の末尾の y を除き、末尾に ies を付ける
・上のいずれの条件にも当てはまらない英単語の末尾には s を付ける入力された英単語を複数形に変換するプログラムを作成してください。
入力は以下のフォーマットで与えられます。
N #入力される単語の数
a_1 #1個目の単語
a_2 #2個目の単語
...
a_N #N個目の単語必ずしも実在の英単語が入力されるわけではありません。
入力値最終行の末尾に改行が1つ入ります。
文字列は標準入力から渡されます。
以上のような設問に対して、以下のコードを提出しましたが、
条件式の中身のボリュームが多く、よりよいコードがあればご教示いただければ幸いです。
###該当のソースコード
Java
1import java.util.Scanner; 2 3public class Main { 4 public static void main(String[] args) { 5 6 Scanner sc = new Scanner(System.in); 7 int N = sc.nextInt(); 8 sc.nextLine(); 9 for (int i = 0; i < N; i++) { 10 String word = sc.nextLine(); 11 int size = word.length(); 12 int cut_length = 2; 13 String end2 = word.substring(size - cut_length); 14 String end1 = end2.substring(1); 15 16 if (end1.equals("s") || end1.equals("o") || end1.equals("x") || end2.equals("sh") || end2.equals("ch")) { 17 word += "es"; 18 }else if (end1.equals("f")) { 19 word = word.replaceFirst(end1, "ves"); 20 }else if (end2.equals("fe")) { 21 word = word.replaceFirst(end2, "ves"); 22 }else if (end1.equals("y") && ( !end2.substring(0, 1).equals("a") || !end2.substring(0, 1).equals("i") || !end2.substring(0, 1).equals("u") || !end2.substring(0, 1).equals("e") || !end2.substring(0, 1).equals("o"))) { 23 word = word.replaceFirst(end1, "ies"); 24 }else { 25 word += "s"; 26 } 27 System.out.println(word); 28 } 29 30 }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/12 08:12