使用队列的 Java Pig 拉丁语句子翻译器 [英] Java Pig Latin sentence translator using Queues

查看:29
本文介绍了使用队列的 Java Pig 拉丁语句子翻译器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Java 非常陌生,正在尝试创建一个程序将一个句子翻译成 Pig Latin,将单词的第一个字母移到末尾,如果第一个字母是元音,则在末尾附加y"并且否则在末尾的ay".我需要为此使用队列.目前我的程序刚刚终止,我想知道是否有人能够发现我哪里出错了或者下一步要去哪里.谢谢!

I am very new to Java and am trying to create a program to translate a sentence into Pig Latin, moving the first letter of the word to the end and appending "y" at the end if the first letter was a vowel and "ay" at the end otherwise. I am required to use a queue for this. Currently my program is just terminating and I was wondering if anyone might be able to spot where I am going wrong or where to head next. Thanks!

import MyQueue.QueueList;导入 java.util.Scanner;

import MyQueue.QueueList; import java.util.Scanner;

公开课 PigLatin{

public class 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 类:

And here is the QueueList class from the MyQueue package:

// 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();
    }

}

推荐答案

在进入第二个 while 循环之前,您没有将索引重置为 0.由于 index == message.length() 在第一个循环结束后,第二个循环立即终止.

You are not resetting index to 0 before entering your second while loop. Since index == message.length() after the first loop ends, the second loop terminates immediately.

回复:您的最新更新.

在您的第二个循环中,您只从单词队列中出列第一个 message.length() 字符.如果您在末尾添加了 -ay,您将看不到它.相反,循环队列的长度,而不是输入消息的长度:

In your second loop you only dequeue the first message.length() characters from the word queue. If you've added -ay to the end you won't see it. Instead, loop on the length of the queue, not the length of the input message:

while (!word.empty())
    System.out.print(word.dequeue());

我可以发现您的逻辑存在许多其他问题(您没有删除第一个字母,也没有处理句子中的单个单词),但是上述更改应该足以让您打印队列中的内容并将其发送给您正在调试中.

I can spot numerous other issues with your logic (you are not removing the first letter and you are not processing individual words in the sentence), but the above changes should be enough to get you printing what's on the queue and send you on your way debugging.

这篇关于使用队列的 Java Pig 拉丁语句子翻译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆