Java Pig Latin程序用于学校

huangapple go评论67阅读模式
英文:

Java Pig Latin program for school

问题

我在为学校的家庭作业中遇到了问题。我们需要编写一个程序,允许用户输入一个句子,然后将其翻译并显示为 Pig Latin。显示部分将在左侧显示原始单词,然后是一个制表符,最后是翻译后的单词。我在将第一个元音前面的字符移到单词末尾的部分卡住了。例如,在单词 "trouble" 中,我无法弄清楚如何将 "tr" 移到单词末尾,使其变为 "oubletr"。我会感谢任何能指引我正确方向的帮助。以下是我目前的代码。

import java.util.Scanner;

public class PigLatinTranslator {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        // 声明 userSentence 和 pigLatin 字符串
        String userSentence;

        // 将元音赋值给字符
        char a = 'a';
        char e = 'e';
        char i = 'i';
        char o = 'o';
        char u = 'u';

        // 向用户询问输入并保存到 userSentence 变量中
        System.out.println("Enter a sentence: ");
        userSentence = input.nextLine();

        // 将 userSentence 拆分为数组
        String[] userWords = userSentence.split(" ");

        // 这将遍历单词并定位元音
        for (int k = 0; k < userWords.length; k++) {

            // 将 userWords 中的字母转换为小写
            String temp = userWords[k].toLowerCase();
            char c = temp.charAt(0);

            // 如果第一个字符等于元音
            if (c == a || c == e || c == i || c == o || c == u) {
                System.out.println(temp + "\t" + temp + "way");
            } else {
                // 打印以辅音开头的单词
                System.out.println(temp + "\t" + temp + "ay");
            }
        }
    }
}
英文:

I am having trouble with my homework for school. We are supposed to write a program that allows a user to input a sentence and then have it translated and displayed in Pig Latin. The display will show the original word on the left side, then a tab, and finally the translated word. I am stuck at the part where the characters in front of the first vowel are moved to the end of the word. For example, in the word "trouble" I can't figure out how to move the 'tr' to the end of the word to make it "oubletr". I would appreciate any help that points me in the right direction. Below is my code so far.

import java.util.Scanner;

public class PigLatinTranslator {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//Declare string for userSentence and pigLatin
String userSentence; 
//Assign vowels to char
char a = &#39;a&#39;;
char e = &#39;e&#39;;
char i = &#39;i&#39;;
char o = &#39;o&#39;;
char u = &#39;u&#39;;
//Ask user for input and save to userSentence variable
System.out.println(&quot;Enter a sentence: &quot;);
userSentence = input.nextLine();
//Split the userSentence into an array
String[] userWords = userSentence.split(&quot; &quot;);
//This will go through the words and locate vowels
for(int k = 0; k &lt; userWords.length; k++) {
//Change letters in userWords to lower case
String temp = userWords[k].toLowerCase();
char c = temp.charAt(0); 
//If first character is equal to a vowel
if(c == a || c == e || c == i || c == o || c == u) {
System.out.println(temp + &quot;\t&quot; + temp + &quot;way&quot;);
}
else {
//print the ones that start with a consonant
System.out.println(temp + &quot;\t&quot; + temp + &quot;ay&quot;);
}
}
}

}

答案1

得分: 1

例如在单词trouble我无法弄清楚如何将tr移动到单词的末尾使其变为oubletr”。

按以下步骤操作

    class Main {
    	public static void main(String[] args) {
    		String str = "trouble", newStr = "";
    		String strLowerCase = str.toLowerCase();
    		for (int i = 0; i < str.length(); i++) {
    			char ch = strLowerCase.charAt(i);
    			if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
    				newStr = str.substring(i) + str.substring(0, i);
    				break;
    			}
    		}
    		System.out.println(newStr);
    	}
    }

**输出结果:**

    oubletr

**备注:**

 1. 将字符串转换为小写并将其字符与小写元音字母`a`、`e`、`i`、`o`、`u`)进行比较或者您可以将字符串转换为大写并将其字符与大写元音字母`A`、`E`、`I`、`O`、`U`)进行比较
 2. `String::substring(start)` 返回从索引 `start` 开始的所有字符的字符串。`String::substring(start, end)` 返回从索引 `start` 开始到索引 `end - 1` 的所有字符的字符串
英文:

> For example, in the word "trouble" I can't figure out how to move the
> 'tr' to the end of the word to make it "oubletr".

Do it as follows:

class Main {
public static void main(String[] args) {
String str = &quot;trouble&quot;, newStr = &quot;&quot;;
String strLowerCase = str.toLowerCase();
for (int i = 0; i &lt; str.length(); i++) {
char ch = strLowerCase.charAt(i);
if (ch == &#39;a&#39; || ch == &#39;e&#39; || ch == &#39;i&#39; || ch == &#39;o&#39; || ch == &#39;u&#39;) {
newStr = str.substring(i) + str.substring(0, i);
break;
}
}
System.out.println(newStr);
}
}

Output:

oubletr

Notes:

  1. Convert the string into the lower case and compare its characters with the lower case vowel letters (i.e. a, e, i, o, u). Alternatively, you can convert the string into the upper case and compare its characters with the upper case vowel letters (i.e. A, E, I, O, U).
  2. String::substring(start) returns a string with all characters starting from the index, start. String::substring(start, end) returns a string with all characters starting from the index, start till the index, end - 1.

答案2

得分: 1

你可以更简便地使用contains()方法,这样就不需要进行两次循环...

String vowels = "aeiou"; // 不确定是否计算y,谁在乎呢
String message = "trouble";
for (int letter = 0; letter < message.length(); letter++) {
if (vowels.contains("" + message.charAt(letter))) {
message = message.substring(letter) + message.substring(0, letter);
break;
}
}
System.out.println(message); // oubletr
英文:

You can make it easier and use the contains() method so you don't have to do two loops...

String vowels = &quot;aeiou&quot;; // idk if y counts who cares
String message = &quot;trouble&quot;;
for (int letter = 0; letter &lt; message.length(); letter++) {
if (vowels.contains(&quot;&quot; + message.charAt(letter))) {
message = message.substring(letter) + message.substring(0, letter);
break;
}
}
System.out.println(message); // oubletr

huangapple
  • 本文由 发表于 2020年4月11日 04:36:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/61148304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定