This code repeats the output after triple spaces. I just want triple spaces after a word, but the output keeps repeating. For example, I just want to see Output: .- (triple spaces) .-;
String english = input.nextLine();
char[] toChar = english.toCharArray();
String output = "";
for (int i = 0; i < toChar.length; i++) {
if (toChar[i] == 'a' || toChar[i] == 'A') {
output += ".-";
}
if (toChar[i] == 'b' || toChar[i] == 'B') {
output += "-...";
}
if (toChar[i] == ' ') {
output += " ";
}
System.out.print(output + " ");
}
Run Code Online (Sandbox Code Playgroud)
Input
a a
Run Code Online (Sandbox Code Playgroud)
Output
.- .- .- .-
Run Code Online (Sandbox Code Playgroud)
您的帖子太混乱了。尝试对以后的帖子更加清楚。但是,由于输出在循环内(对输入数据的每个字符重复),因此将重复输出。因此,将您的System.out(....);外部循环。
String english = input.nextLine();
char[] toChar = english.toCharArray();
String output = "";
for (int i = 0; i < toChar.length; i++) {
if (toChar[i] == 'a' || toChar[i] == 'A') {
output += ".-";
}
if (toChar[i] == 'b' || toChar[i] == 'B') {
output += "-...";
}
if (toChar[i] == ' ') {
output += " ";
}
}
System.out.print(output + " ");
Run Code Online (Sandbox Code Playgroud)