我正在尝试编写一些带字符串的代码并将其翻译成这样; 1.取第一个字母,并将其放在每个单词的单词末尾 2.找到第一个元音并再放一个'b'然后再放元音 3.除最后一个元音
外,与#2相同我想我有点接近,但我的输出是所有数字。它甚至看起来都不像存储地址 我希望这可以帮助其他人,因为他们可能在return语句中打印数组列表时遇到同样的问题。 顺便说一句,这是一个巨大的代码块...抱歉。我这样做的唯一原因是我没有把这两个课程放在这里。
以下是代码:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Experiment {
public static void main(String[] args){
String pre = "For every minute you are angry you loose sixty seconds of happiness.";
System.out.println(translate(pre));
}
public static String translate(String sentence){
String[] sentenceArray = sentence.split(" ");
List<String> sentenceList = new ArrayList<>();
List<String> finalList = new ArrayList<>();
String punctuation = getPunctuation(sentenceArray[sentenceArray.length - 1]);
//add all words but the last so i can take punctuation off it
for (int i = 0; i < sentenceArray.length - 1; i ++){
sentenceList.add(sentenceArray[i]);
}
//take the first letter off each word and put at at the end of each word
Arrays.asList(sentenceArray);
for (String el : sentenceArray)
sentenceList.add(firstToLast(el));
//use the addFrontB method on each word
Arrays.asList(sentenceList);
for (String la : sentenceList){
finalList.add(addFrontB(la));
}
//use the addBackB method on each word
Arrays.asList(sentenceList);
for (String le : sentenceList){
finalList.add(addBackB(le));
}
return finalList + punctuation + "\n";
}
//finds the last character of the last word which is punctuation
public static String getPunctuation(String word){
return word.charAt(word.length() - 1) + "";
}
//takes the punctuation off
public static String removePunctuation(String word){
String newWord;
newWord = word.substring(word.length(), word.length());
return newWord;
}
//puts the first letter at the end of the word
public static String firstToLast(String word){
char letter = word.charAt(0);
String newWord = word.substring(1,word.length()) + letter;
return newWord;
}
//insterts a b and then the same vowel behind the first vowel
public static String addFrontB(String word){
StringBuilder finishedWord = new StringBuilder();
for (int i = 0; i < word.length(); i ++){
if (word.charAt(i) == 'a')
finishedWord = finishedWord.append(word.charAt(i) + 'b' + word.charAt(i));
else if (word.charAt(i) == 'e')
finishedWord = finishedWord.append(word.charAt(i) + 'b' + word.charAt(i));
else if (word.charAt(i) == 'i')
finishedWord = finishedWord.append(word.charAt(i) + 'b' + word.charAt(i));
else if (word.charAt(i) == 'o')
finishedWord = finishedWord.append(word.charAt(i) + 'b' + word.charAt(i));
else if (word.charAt(i) == 'u')
finishedWord = finishedWord.append(word.charAt(i) + 'b' + word.charAt(i));
}
String newWord = finishedWord.toString();
return newWord;
}
//does the same as addFirstB but at the end of the word
public static String addBackB(String word){
StringBuilder finishedWord = new StringBuilder();
finishedWord.append(word);
finishedWord.reverse();
for (int i = 0; i < word.length(); i ++){
if (finishedWord.charAt(i) == 'a')
finishedWord.append(finishedWord.charAt(i) + 'b').reverse();
else if (finishedWord.charAt(i) == 'e')
finishedWord.append(finishedWord.charAt(i) + 'b').reverse();
else if (finishedWord.charAt(i) == 'i')
finishedWord.append(finishedWord.charAt(i) + 'b').reverse();
else if (finishedWord.charAt(i) == 'o')
finishedWord.append(finishedWord.charAt(i) + 'b').reverse();
else if (finishedWord.charAt(i) == 'u')
finishedWord.append(finishedWord.charAt(i) + 'b').reverse();
}
return finishedWord.toString();
}
}
答案 0 :(得分:0)
return finalList + punctuation + "\n";
您正在将List
视为此处的字符串。您是否需要执行.toString()
或类似的事情?
asList(T... a)
Returns a fixed-size list backed by the specified array.
您还没有使用Arrays.asList
答案 1 :(得分:0)
你的错误在这里:
return finalList + punctuation + "\n";
由于你使用plus运算符添加“\ n”,java会调用列表的toString()方法,这会返回废话。您必须遍历列表并构建自己的字符串,最佳做法是使用StringBuffers。