我对Java非常陌生,正在尝试创建一个程序将句子翻译成Pig Latin,将单词的第一个字母移到末尾,如果第一个字母是元音,则在末尾附加“y”和“ay”最后否则。我需要为此使用队列。目前我的程序刚刚结束,我想知道是否有人能够发现我哪里出错或下一步要去哪里。谢谢!
导入 MyQueue.QueueList;导入 java.util.Scanner;
公共类 PigLatin {
public static void main (String[] args)
{
Scanner scan = new Scanner (System.in);
QueueList word = new QueueList();
String message;
int index = 0;
char firstch;
System.out.print ("Enter an English sentence: ");
message = scan.nextLine();
System.out.println ("The equivalent Pig Latin sentence is: ");
firstch = Character.toLowerCase(message.charAt(0));
if (firstch != 'a' && firstch != 'e' && firstch != 'i' && firstch != 'o' && firstch != 'u'
&& firstch != ' ')
{
for (index = 1; index < message.length(); index++)
{
word.enqueue(new Character(message.charAt(index)));
}
word.enqueue(new Character (firstch));
word.enqueue(new Character ('a'));
word.enqueue(new Character ('y'));
word.enqueue(new Character(' '));
}
else if (firstch == 'a' || firstch == 'e' || firstch == 'i' || firstch == 'o' || firstch == 'u')
{
while (message.charAt(index) != ' ')
{
for (index = 1; index < message.length(); index++)
{
word.enqueue(new Character(message.charAt(index)));
}
}
word.enqueue((firstch));
word.enqueue( ('y'));
word.enqueue((' '));
}
else if (message.charAt(index) == ' ')
{
index++;
firstch = message.charAt(index);
}
while (!word.empty())
System.out.print(word.dequeue());
}
}
这是 MyQueue 包中的 QueueList 类:
// QueueList.java
//
// Class QueueList definition with composed List object.
package MyQueue;
public class QueueList {
private List a_queue;
public QueueList() {
a_queue = new List( "queue" );
}
public Object peek() throws EmptyListException {
if (a_queue.isEmpty())
return null;
else
return a_queue.getFirstObject();
}
public void print() {
a_queue.print();
}
public void enqueue(Object object) {
a_queue.insertAtBack(object);
}
public Object dequeue() throws EmptyListException {
return a_queue.removeFromFront();
}
public boolean empty() {
return a_queue.isEmpty();
}
}