末尾の1文字が y で、末尾から2文字目が a, i, u, e, o のいずれかの場合にsがつかない
-入力値-
2
study
play
期待する出力値
studies
plays
出力結果
studies
plaies
意見
ロジック的には間違っていないと思うのですが、不備があるかもしれません。30分見ても問題解消に至りませんでした。
java
1import java.util.*; 2 3 4public class Main { 5 public static void main(String[] args) { 6 Scanner sc = new Scanner(System.in); 7 8 int N = sc.nextInt(); //英語単語数 9 if(N >= 1 && N<=10){ //入力範囲 10 11 sc.nextLine(); 12 13 for(int i=0; i<N; i++){ //指定した英単語数分ループ 14 15 String word =sc.nextLine(); //複数形にする英単語 16 System.out.println(Grammarcheck(word)); //文法チェック後の出力 17 18 } 19 } 20 } 21 22 public static String Grammarcheck(String word){ //文法チェックと変換する関数 23 if(word.endsWith("s") || word.endsWith("sh") || word.endsWith("ch") || word.endsWith("o") || word.endsWith("x")){ //末尾が s, sh, ch, o, x のいずれかの場合 24 25 return word + "es"; //英単語の末尾にesを追記 26 27 } 28 if(word.endsWith("f")){ //末尾がfの場合 29 30 return word.replace("f", "ves"); //英単語の末尾のfを除き、末尾に ves を付ける 31 32 } 33 if(word.endsWith("fe")){ //末尾がfeの場合 34 35 return word.replace("fe", "ves"); //英単語の末尾のfeを除き、末尾に ves を付ける 36 37 } 38 if(word.endsWith("y")){//末尾の1文字が yの場合 39 40 char charEnd2 = word.charAt(word.length() - 2); //末尾から2文字目取得 41 String end2 = String.valueOf(charEnd2); //String型へ変換 42 43 if(end2 != "a" || end2 != "i" || end2 != "u" || end2 != "e" || end2 != "o"){ //末尾から2文字目が a, i, u, e, o のいずれでもない場合 44 45 return word.replace("y","ies"); //英単語の末尾の y を除き、末尾に ies を付ける 46 47 } 48 } 49 //上のいずれの条件にも当てはまらない場合 50 return word + "s"; //英単語の末尾には s を付ける 51 } 52 53} 54 55
回答5件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。