我正在尝试编写一些代码,它需要一个字符串并像这样翻译它;1. 将第一个字母放在每个单词的词尾 2. 找到第一个元音并放一个“b”,然后再放元音 3. 除了最后一个元音外,与 #2 相同
我想我已经接近了,但我的输出都是数字。它甚至看起来不像它的存储地址。
我希望这对其他人有所帮助,因为他们可能在返回语句中打印数组列表时遇到同样的问题。顺便说一句,这是一个巨大的代码块......对不起。我这样做的唯一原因是我不必将两个类都放在这里。
这是代码:
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();
}
}