简体   繁体   English

我的代码做错了什么? 翻译成 PigLatin 并无法弄清楚如何打印返回值

[英]What am I doing wrong with my code? Translating into PigLatin and cant figure out how to print returned value

import java.util.*;
public class Project3 {
    public static void main (String[] args) {

    ArrayList<String> translationOfPigLatin = new ArrayList<String>();
    Scanner Scanner = new Scanner(System.in);
    translationOfPigLatin.add(Scanner.next());
    StringBuffer sb = new StringBuffer();
      for(int i = 0; i < pigLatin.length; i++) {
        System.out.println(pigLatin[i]);

      } 
    }
    public static String translateToPigLatin(String pigLatin){
    int num = 0;
    String translationToPigLatin = pigLatin;

    if (pigLatin.indexOf('a') == num){
        System.out.print(pigLatin + "ay");
            return pigLatin;
       } else if (pigLatin.indexOf('e') == num){
        System.out.println(pigLatin + "ay");
            return pigLatin;
       } else if (pigLatin.indexOf('i') == num){
       System.out.println(pigLatin + "ay");
            return pigLatin;
       } else if (pigLatin.indexOf('o') == num){
        System.out.println(pigLatin + "ay");
            return pigLatin;
       } else if (pigLatin.indexOf('u') == num){
        System.out.println(pigLatin + "ay");
            return pigLatin;
       } else {
        char firstLetter = pigLatin.charAt(0); 
        pigLatin = pigLatin.substring(1); 
        System.out.println(pigLatin + firstLetter + "ay");
            return pigLatin; 
        }
    }
}

I dont know how to return the pigLatin value to be able to print it out.我不知道如何返回 pigLatin 值才能打印出来。 The instructions for my code was: Your program must take in a sentence (of any length) as command line arguments.我的代码的说明是:你的程序必须接受一个句子(任意长度)作为命令行参数。 Your program must contain a method named translateToPigLatin() that takes in an array of Strings as the argument (the input), and returns a single String (the Pig Latin translation).您的程序必须包含一个名为 translateToPigLatin() 的方法,该方法接受字符串数组作为参数(输入),并返回单个字符串(Pig Latin 翻译)。 Each word in the sentence must be translated to Pig Latin using the rules above.必须使用上述规则将句子中的每个单词翻译成 Pig 拉丁语。 You must print the sentence's Pig Latin translation back out on one line.您必须将句子的 Pig Latin 翻译打印回一行。 You can ignore punctuation and capitalization, and assume all test input will only consist of lowercase letters and spaces.您可以忽略标点符号和大写字母,并假设所有测试输入仅由小写字母和空格组成。

Let's take this step by step.让我们一步一步来。 Let's forget about everything else for a moment and center on the translateToPigLatin function.让我们暂时忘记其他所有事情,并以translateToPigLatin函数为中心。

We need to create a function that takes an String and outputs another, which will be its Pig Latin equivalent.我们需要创建一个函数,它接受一个String并输出另一个,这将是它的 Pig Latin 等价物。 You almost nailed that down.你几乎把它搞定了。 Here is such function with just a couple adjustments over your original code:这是这样的功能,只需对原始代码进行一些调整:

    // Let's write a function that takes an input string and outputs pigLatin translation
    public static String translateToPigLatin(String pigLatin) {
        int num = 0;
        // This translationToPigLatin variable ONLY EXISTS between the start and end {} of this function.
        // You are not really using it inside this function, so let's forget about it.
        // String translationToPigLatin = pigLatin;

        // Instead of writing output now. Let the function just do the transform and we'll do output later on.
        // I've commented out all the System.out.print
        if (pigLatin.indexOf('a') == num) {
            //System.out.print(pigLatin + "ay");

            // Whenever we do a return statement, we need to put the value that we want the function to return.
            // In your code, you called System.out.print with the appropriate value but returned just the pigLatin
            // variable which would contain the original word. We need to return the translated value instead. 
            return pigLatin + "ay";
        } else if (pigLatin.indexOf('e') == num) {
            //System.out.println(pigLatin + "ay");
            return pigLatin + "ay";
        } else if (pigLatin.indexOf('i') == num) {
            //System.out.println(pigLatin + "ay");
            return pigLatin + "ay";
        } else if (pigLatin.indexOf('o') == num) {
            //System.out.println(pigLatin + "ay");
            return pigLatin + "ay";
        } else if (pigLatin.indexOf('u') == num) {
            //System.out.println(pigLatin + "ay");
            return pigLatin + "ay";
        } else {
            char firstLetter = pigLatin.charAt(0);
            pigLatin = pigLatin.substring(1);
            //System.out.println(pigLatin + firstLetter + "ay");
            return pigLatin + firstLetter + "ay";
        }
    }

I don't know if this is the translation you need because you forgot to include the rules but this would be a working version of your translation function.我不知道这是否是您需要的翻译,因为您忘记包含规则,但这将是您的翻译功能的工作版本。

Now let's clear it up a bit.现在让我们把它弄清楚一点。

First, the input variable name.首先,输入变量名。 Let's just call it input so we know what it stands for the whole time.让我们称之为input这样我们就知道它代表什么。

    public static String translateToPigLatin(String input) {

We can get rid of the int num = 0 .我们可以去掉int num = 0 If you need to check the start of the word it'd be more practical to do something like this:如果您需要检查单词的开头,这样做会更实用:

        // Create a variable that holds the first character
        char firstLetter = input.charAt(0);

Now, since you have 5 cases in which your if would do the same thing, let's write a single condition for all of those.现在,由于您有 5 种情况,其中 if 会做同样的事情,让我们为所有这些情况编写一个条件。

        // In Java || is the logical OR operator. This condition will be true if the first predicate is true OR
        // the second OR the third...
        // In other words. If any of these comparisons are true. The condition will be true.
        if (firstLetter == 'a' || firstLetter == 'e' || firstLetter == 'i' || firstLetter == 'o' || firstLetter == 'u') {
            // And then we'll do this.
            return input + "ay";
        }

Since we already have the first letter we can write the else such as:因为我们已经有了第一个字母,所以我们可以写else例如:

        else {
            // A variable with all but the first letter
            String noFirstLetter = input.substring(1);
            // And return the piglatin translation
            return noFirstLetter + firstLetter + "ay";
        }

Putting it all together looks like this:把它们放在一起看起来像这样:

    public static String translateToPigLatin(String input) {
        char firstLetter = input.charAt(0);

        if (firstLetter == 'a' || firstLetter == 'e' || firstLetter == 'i' || firstLetter == 'o' || firstLetter == 'u') {
            return input + "ay";
        } else {
            String noFirstLetter = input.substring(1);
            return noFirstLetter + firstLetter + "ay";
        }
    }

The only thing that does is transform the information contained in input somehow and return the result of that transformation.唯一能做的就是以某种方式转换input包含的信息并return该转换的结果。 Now that we have that let's forget everythin about how it works and consider it a blackbox of a kind.现在我们有了它,让我们忘记它是如何工作的一切,把它看作是一种黑匣子。 We'll just know that if you do String output = translateToPigLatin("something") we'll get omethingsay as the output.我们只知道如果你执行String output = translateToPigLatin("something")我们将得到omethingsay作为输出。

This is a way we could use that to make what your exercise describes.这是我们可以用来制作您的练习所描述的内容的一种方式。

    public static void main(String[] args) {
        // We need to process the console input. The way you originally used is indeed a good way to do that.
        // Notice the lowercase scanner. In Java, by convention, variables are named starting lowercase.
        Scanner scanner = new Scanner(System.in);

        // Every scanner.next call we'll return an input word. Knowing that we just need to accumulate those.
        // A List seems appropriate for that task.
        List<String> list = new ArrayList<>();

        // As we don't know how many input strings we are receiving we'll just have to rely on scanner's hasNext method
        // to known when we are done. When there's nothing more to read, hasNext will return false, ending our loop.
        while(scanner.hasNext()) {
            String word = scanner.next();
            // We'll immediately translate every received word using our translation function
            String translation = translateToPigLatin(word);
            // Then store the translation and repeat
            list.add(translation);
        }

        // When we reach the end of the words we then proceed to write them in the console in one line, separated by
        // spaces. We can use String.join to generate a big String containing every word on list, separated by spaces.
        String result = String.join(" ", list);
        // And print it out
        System.out.println(result);
    }

After writing a sequence manually, you need to use Ctrl+D if in Linux or Ctrl+Z and then in Windows to tell the program you are done.手动编写序列后,如果在 Linux 或 Ctrl+Z 中使用 Ctrl+D,然后在 Windows 中告诉程序您已完成,则需要使用 Ctrl+D。 I hope this was useful.我希望这是有用的。 If it solves your problem, please don't forget to mark my answer as solution so it can help more people in the future.如果它解决了您的问题,请不要忘记将我的答案标记为解决方案,以便将来可以帮助更多人。

First of all you need to know that one can not use reserved words as variable names.首先你需要知道一个不能使用保留字作为变量名。 So, first change the code as follows:因此,首先将代码更改如下:

Scanner scanner = new Scanner(System.in);// not as Scanner Scanner.

and then in the main function you don't have any variable named pigLatin and hence you will have no access to it.然后在主函数中,您没有任何名为pigLatin 的变量,因此您将无法访问它。 And as per I can understand your question statement you need to have array of strings as parameter in the translateToPigLatin method.根据我可以理解您的问题陈述,您需要将字符串数组作为translateToPigLatin方法中的参数。 For returning pigLatin array you can save the each string's pigLatin into a variable and assemble them into an array variable and return that array variable.对于返回 pigLatin 数组,您可以将每个字符串的 pigLatin 保存到一个变量中并将它们组装成一个数组变量并返回该数组变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM