我正在制作一个Pig Latin翻译器,将用户的输入翻译成Pig Latin。我知道这个单词以元音开头的时候以及当它没有把第一个字母放在中间时。然而,当谈到辅音簇(单词中第一个元音之前的字符组)时,我无法弄清楚如何将它们组合成自己的变量。我正在使用for循环来扫描第一个变量的字母,然后尝试将所有这些字符串聚集到它自己的变量中然后放入单词的中间。
这是我到目前为止的代码:
import java.util.Scanner;
public class Mission4
{
public static void main(String[] args)
{
Scanner in = new Scanner (System.in);
System.out.println("Please enter word to convert to Piglatin:");
String userInput = in.nextLine();
int firstV = 0;
char firstCh = Character.toLowerCase(userInput.charAt(0));
do
{
if (firstCh == 'a' || firstCh == 'e' || firstCh == 'i' || firstCh == 'o' || firstCh == 'u') //if userInput starts with vowel
{
String pigTalk = userInput + "ay";
System.out.println(pigTalk); // adding 'ay' to the end of their input
System.out.println("Enter another word to convert to piglatin, otherise press \"Q\" to exit.");
userInput = in.nextLine();
}
else //if userInput doesn't begin with vowel
{
for (int i = 0; i < firstV; i++)
{
char ch = userInput.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
{
firstV = Character.subString(ch);
}
}
String firstCluster = userInput.substring(0,firstV.length);
String secondCluster = userInput.substring(firstV.length,userInput.length());
System.out.println(secondCluster + firstCluster + "ay"); //Printing out their piglatin
System.out.println("Enter another word, or type \"Q\" to exit program.");
userInput = in.nextLine();
}
} while (!userInput.equals("Q")); //Giving user an exit option
}
}
你能提供任何建议吗?任何帮助表示赞赏。
答案 0 :(得分:1)
首先,尝试将代码组织成更小的函数,形成逻辑单元。这使您和其他人更容易理解程序正在做什么,以及它做错了什么。
你的计划中有几个错误:
for (int i = 0; i < firstV; i++)
设置为0,因此此循环立即停止:public String toPigLatin(String word) {
if (isVowel(word.charAt(0))) {
return word + "ay"; // actually, wikipedia says you should append "yay", not "ay"
}
for (int i = 1; i < word.length(); i++) {
if (isVowel(word.charAt(i))) {
return word.substring(i) + word.substring(0, i) + "ay";
}
}
return word + "ay";
}
public boolean isVowel(char c) {
c = Character.toLowerCase(c);
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
循环需要做的是从第二个字符迭代,直到找到元音。
这里是提取到函数的已校正主逻辑,也是辅助函数:
var data = [{
"ConferenceUsers": [{
"Id": 3006,
"ConferenceId": 8,
"Name": null,
"Email": "mail@lala.com",
"UserName": null,
"PhoneNumber": "234234234234"
}],
"Id": 8,
"Subject": "YYYhaaaaa",
"StartTime": "2016-05-29T18:30:00",
"EndTime": "2016-05-29T19:30:00",
"OrganizerEmail": "elpas@live.com",
"OrganizerName": "dasdasd",
}, {
"ConferenceUsers": [{
"Id": 3013,
"ConferenceId": 12,
"Name": null,
"Email": "dsfdfsdfdsf@dfdfdf.com",
"UserName": null,
"PhoneNumber": null
}],
"Id": 12,
"Subject": "dsfsdfdsfsdf",
"StartTime": "2016-05-31T22:00:00",
"EndTime": "2016-05-31T23:00:00",
"OrganizerEmail": "d@adssad.com",
"OrganizerName": "dsfdsfsdf"
}];
答案 1 :(得分:0)
这是一个将String转换为Pig Latin的程序:
import java.io.*;
class Test1
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
System.out.print("Enter any word: ");
String s=br.readLine();
s=s.toUpperCase(); //converting the word into Uppercase
int l=s.length();
int pos=-1;
char ch;
for(int i=0; i<l; i++)
{
ch=s.charAt(i);
if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
pos=i; //storing the index of the first vowel
break;
}
}
if(pos!=-1) //printing piglatin only if vowel exists
{
String a=s.substring(pos); //extracting all alphabets in the word beginning from the 1st vowel
String b=s.substring(0,pos); //extracting the alphabets present before the first vowel
String pig=a+b+"AY"; //adding "AY" at the end of the extracted words after joining them
System.out.println("The Piglatin of the word = "+pig);
}
else
System.out.println("No vowel, hence piglatin not possible");
}
}