7-3 sdut-JAVA-Common Plurals
分数 9
全屏浏览
切换布局
作者 马新娟
单位 山东理工大学
When forming plurals of common nouns that end in y in English the following rules are observed:
• Common nouns ending in y preceded by a vowel form their plurals by adding s, for example, bay, bays; key, keys; toy, toys.
• Common nouns ending in y preceded by a consonant or by qu change the y to i and add es, for example, city, cities; faculty, faculties; soliloquy, soliloquies; You should accept a word from the end user and if its plural can be formed using the rules specified here you should output the word input and its plural form otherwise, you should state that the plural form of the word cannot be formed using these rules.
Input Specification:
Accept a word from the end user.
Output Specification:
If its plural can be formed using the rules specified here you should output the word input and its plural form otherwise, you should state that the plural form of the word cannot be formed using these rules.
Sample Input:
bay
Sample Output:
bay in plural form is: bays
student
Sample Output:
Cannot form the plural form with the rules given.
soliloquy
Sample Output:
soliloquy in plural form is: soliloquies
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String arr = sc.nextLine(); int n = arr.length(); if(arr.charAt(n-1) != 'y'){ System.out.println("Cannot form the plural form with the rules given."); System.exit(0); } else{ if(arr.charAt(n-3) == 'q' && arr.charAt(n-2) == 'u'){ System.out.print(arr+" in plural form is: " ); for(int i = 0;i<arr.length()-1;i++){ System.out.print(arr.charAt(i)); } System.out.println("ies"); System.exit(0); } else if(arr.charAt(n-2) == 'a' || arr.charAt(n-2) == 'e' ||arr.charAt(n-2) == 'i' ||arr.charAt(n-2) == 'o' ||arr.charAt(n-2) == 'u' ){ System.out.println(arr+" in plural form is: " +arr+ "s"); } else{ System.out.print(arr+" in plural form is: " ); for(int i = 0;i<arr.length()-1;i++){ System.out.println(arr.charAt(i)); } System.out.println("ies"); System.exit(0); } } } }