英文:
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 = 'a';
char e = 'e';
char i = 'i';
char o = 'o';
char u = 'u';
//Ask user for input and save to userSentence variable
System.out.println("Enter a sentence: ");
userSentence = input.nextLine();
//Split the userSentence into an array
String[] userWords = userSentence.split(" ");
//This will go through the words and locate vowels
for(int k = 0; k < 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 + "\t" + temp + "way");
}
else {
//print the ones that start with a consonant
System.out.println(temp + "\t" + temp + "ay");
}
}
}
}
答案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 = "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);
}
}
Output:
oubletr
Notes:
- 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
). 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 = "aeiou"; // idk if y counts who cares
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论