大家在字词之间加空格时遇到麻烦。我需要做的是将英语转换为我已经完成的Vogon,但是您还必须在每5个字符处添加空格,这是我的代码。
public static void main(String[] args) {
translateFromEnglish("hello");
}
public static boolean detect(String phrase) {
return false;
}
public static String translateFromEnglish(String phrase){
Scanner scan = new Scanner(System.in);
phrase = scan.nextLine();
String NewString = "";
for (int i = 0; i < phrase.length(); i++) {
if (phrase.charAt(i) == 'a') {
NewString += "n";
}
if (phrase.charAt(i) == 'b') {
NewString += "o";
}
if (phrase.charAt(i) == 'c') {
NewString += "p";
}
if (phrase.charAt(i) == 'd') {
NewString += "q";
}
if (phrase.charAt(i) == 'e') {
NewString += "r";
}
if (phrase.charAt(i) == 'f') {
NewString += "s";
}
if (phrase.charAt(i) == 'g') {
NewString += "t";
}
if (phrase.charAt(i) == 'h') {
NewString += "u";
}
if (phrase.charAt(i) == 'i') {
NewString += "v";
}
if (phrase.charAt(i) == 'j') {
NewString += "w";
}
if (phrase.charAt(i) == 'k') {
NewString += "x";
}
if (phrase.charAt(i) == 'l') {
NewString += "y";
}
if (phrase.charAt(i) == 'm') {
NewString += "z";
}
if (phrase.charAt(i) == 'n') {
NewString += "a";
}
if (phrase.charAt(i) == 'o') {
NewString += "b";
}
if (phrase.charAt(i) == 'p') {
NewString += "c";
}
if (phrase.charAt(i) == 'q') {
NewString += "d";
}
if (phrase.charAt(i) == 'r') {
NewString += "e";
}
if (phrase.charAt(i) == 's') {
NewString += "f";
}
if (phrase.charAt(i) == 't') {
NewString += "g";
}
if (phrase.charAt(i) == 'u') {
NewString += "h";
}
if (phrase.charAt(i) == 'v') {
NewString += "i";
}
if (phrase.charAt(i) == 'w') {
NewString += "j";
}
if (phrase.charAt(i) == 'x') {
NewString += "k";
}
if (phrase.charAt(i) == 'y') {
NewString += "l";
}
if (phrase.charAt(i) == 'z') {
NewString += "m";
}
}
System.out.println(NewString);
return phrase;
}
安慰
你好,你好吗//用户输入
uryybubjnerlbhqbvat //新词组
最佳答案
维护一个计数器,并在for循环中将其递增。在所有if语句的末尾
count++;
if(count % 5 == 0){
NewString += " ";
}