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

查看:178
本文介绍了使用Pig的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.

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

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