Как получить правильный вывод для Pig Latin переводчик с использованием JAVA - PullRequest
0 голосов
/ 12 апреля 2019

Я пишу JAVA-приложение, которое переводит с английского на латинский язык. Мое приложение работает без реальных ошибок, но вывод происходит автоматически и неверно. Это приложение продолжит работу, если пользователь выберет «y».

Можете ли вы все увидеть, где моя ошибка?

Спасибо.

КОД:

import java.util.Scanner;
public class PigLatin2 {

public static void main(String[] args) {

 // create a Scanner object
    Scanner sc = new Scanner(System.in);

    // Run through the loop of calculations while user choice is equal to "y" or "Y"
    String choice = "y";

    while (choice.equalsIgnoreCase("y")) {
        // get the input from the user
        System.out.println("Enter a line to be translated");

        System.out.println();

        //Get String entered
        String userInput = sc.toString();
        //Line break
        System.out.println();

        String[] words = userInput.split(" ");

        String output = "";

        for(int i = 0; i < words.length; i++) {
            String pigLatin = translated(words[i]);
            output += pigLatin + " ";
        }

        System.out.println(output);

        //Scan next line
        sc.nextLine();

        //line break
        System.out.println();

        // Ask use they want to continue
        System.out.print("Continue? (y/n): ");

        //Users choice
        choice = sc.nextLine();
        System.out.println();
    }//END WHILE LOOP

    //Close scanner object
    sc.close();

}//END MAIN METHOD

private static String translated(String words) {
    String lowerCase = words.toLowerCase();
    int firstVowel = -1;
    char ch;

    // This for loop finds the index of the first vowel in the word
    for (int i = 0; i < lowerCase.length(); i++) {
        ch = lowerCase.charAt(i);

        if (startsWithVowel(ch)) {
            firstVowel = i;
            break;
        }
    }

    if (firstVowel == 0) {
        return lowerCase + "way";
    }else {
         String one = lowerCase.substring(firstVowel);
         String two = lowerCase.substring(0, firstVowel);
         return one + two + "ay";
    }
}

    public static Boolean startsWithVowel(char ch) {
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'y') {
            return true;
        }
        return false;
    }
}

Это вывод, который я получаю автоматически:

ava.util.scanner [delimiters = \ p {javawhitespace} +] [position = 0] [matchjay alid = false] [needvay input = false] [sourceway osed = false] [пропущено = false] [groupclay eparator = | \ e] заблудился

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...