7-1 sdut-JAVA-Pig Latin
分数 12
全屏浏览
切换布局
作者 马新娟
单位 山东理工大学
Write a program that requests a word as input, translates the word into Pig Latin and outputs the word input and its equivalent in Pig Latin. The rules for translating a word are as follows:
Rule 1: If a word begins with a consonant, move the first letter to the end of the word and add ay to the end of the word. For example, Chip becomes hipCay.
Rule 2: If the word begins with a vowel, add way to the end of the word. For example, else becomes elseway.
Input Specification:
Request a word as input.
Output Specification:
Outputs the word input and its equivalent in Pig Latin.
Sample Input1:
Sample Output1:
No input provided to convert to Pig Latin.
Sample Input2:
123anksO
Sample Output2:
Input should comprise of alphabetic characters only.
Sample Output3:
123anksO
Sample Output3:
Input should comprise of alphabetic characters only.
Sample Output4:
day
Sample Output4:
Word Input: day
Pig Latin: ayday
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); String arr=in.nextLine(); if(arr.isBlank()) { System.out.println("No input provided to convert to Pig Latin."); return; } int n=arr.length(); for(int i=0;i<n;i++) { if(arr.charAt(i)>='1'&&arr.charAt(i)<='9') { System.out.println("Input should comprise of alphabetic characters only."); return; } } if(arr.charAt(0)=='a'||arr.charAt(0)=='e'||arr.charAt(0)=='i'||arr.charAt(0)=='o'||arr.charAt(0)=='u'||arr.charAt(0)=='A'||arr.charAt(0)=='E'||arr.charAt(0)=='I'||arr.charAt(0)=='O'||arr.charAt(0)=='U') { System.out.println("Word Input: "+arr); System.out.print("Pig Latin: "+arr+"way"); } else{ System.out.println("Word Input: "+arr); System.out.print("Pig Latin: "); for(int i=1;i<n;i++) { System.out.print(arr.charAt(i)); } System.out.println(arr.charAt(0)+"ay"); } } }