如何使用 java 中的嵌套 for 循环在每个具有限制的字符后输出连字符
How to output a hyphen after each character with restrictions using nested for loops in java
我在每个字符(包括下面代码中显示的三个点)后输出一个连字符时遇到一个小问题
示例输入
2 (option #)
disappear (phrase)
预期输出:
d-i-s-a-p-p-e-a-r-.-.-.
d-i-s-a-p-p-e-a-.-.-.
d-i-s-a-p-p-e-.-.-.
d-i-s-a-p-p-.-.-.
d-i-s-a-p-.-.-.
d-i-s-a-.-.-.
d-i-s-.-.-.
d-i-.-.-.
d-.-.-.
.-.-.
.-.
.
它在除最后一个点之外的每个字符后输出“-”
我在每个单词字符后显示“-”,但也无法弄清楚在点后显示,就像它有效但必须少一个连字符:
我的实际输出:
d-i-s-a-p-p-e-a-r-.-.-.-
d-i-s-a-p-p-e-a-.-.-.-
d-i-s-a-p-p-e-.-.-.-
d-i-s-a-p-p-.-.-.-
d-i-s-a-p-.-.-.-
d-i-s-a-.-.-.-
d-i-s-.-.-.-
d-i-.-.-.-
d-.-.-.-
.-.-.-
.-.-
.-
我已经部分完成了,我只需要少一个连字符,它会自动满足在最后一个点后不显示连字符的要求。
代码:
else if (option == 2){
for (int x = 0; x < phrase.length(); x++){
for (int y = 0; y < phrase.length() - x; y++){
char n = phrase.charAt(y);
System.out.print(n+"-");
}
for (int a = 0; a < 3; a++){
System.out.print("."+"-");
}
System.out.println("");
}
for (int j = 0; j < 3; j++){
for (int i = 0; i < 3 - j; i++){
System.out.print("."+"-");
}
System.out.println("");
}
}
删除最后一个 -
的一种方法是仅在 不是最后一个迭代 时打印 -
。您可以通过检查 loopVariable != loopBound - 1
.
来检查这不是最后一次迭代
因此您的代码将是:
for (int x = 0; x < phrase.length(); x++){
for (int y = 0; y < phrase.length() - x; y++){
char n = phrase.charAt(y);
System.out.print(n+"-");
}
// You could just print the literal "--.-." instead
for (int a = 0; a < 3; a++){
System.out.print(".");
if (a != 2) { // Notice here!
System.out.print("-");
}
}
System.out.println();
}
for (int j = 0; j < 3; j++){
for (int i = 0; i < 3 - j; i++){
System.out.print(".");
if (i != 2 - j) { // and here
System.out.print("-");
}
}
System.out.println();
}
我会这样做:
// pretend that the phrase is 3 characters longer than it actually is...
// because of the three dots at the end
for (int x = 0; x < phrase.length() + 3; x++){
for (int y = 0; y < phrase.length() + 3 - x; y++){
char n;
// Should we print the phrase or the dots?
if (y < phrase.length() - x) {
n = phrase.charAt(y);
} else {
n = '.';
}
System.out.print(n);
if (y != phrase.length() + 2 - x) { // same trick of checking if it is last iteration
System.out.print("-");
}
}
System.out.println();
}
我在每个字符(包括下面代码中显示的三个点)后输出一个连字符时遇到一个小问题
示例输入
2 (option #)
disappear (phrase)
预期输出:
d-i-s-a-p-p-e-a-r-.-.-.
d-i-s-a-p-p-e-a-.-.-.
d-i-s-a-p-p-e-.-.-.
d-i-s-a-p-p-.-.-.
d-i-s-a-p-.-.-.
d-i-s-a-.-.-.
d-i-s-.-.-.
d-i-.-.-.
d-.-.-.
.-.-.
.-.
.
它在除最后一个点之外的每个字符后输出“-”
我在每个单词字符后显示“-”,但也无法弄清楚在点后显示,就像它有效但必须少一个连字符:
我的实际输出:
d-i-s-a-p-p-e-a-r-.-.-.-
d-i-s-a-p-p-e-a-.-.-.-
d-i-s-a-p-p-e-.-.-.-
d-i-s-a-p-p-.-.-.-
d-i-s-a-p-.-.-.-
d-i-s-a-.-.-.-
d-i-s-.-.-.-
d-i-.-.-.-
d-.-.-.-
.-.-.-
.-.-
.-
我已经部分完成了,我只需要少一个连字符,它会自动满足在最后一个点后不显示连字符的要求。
代码:
else if (option == 2){
for (int x = 0; x < phrase.length(); x++){
for (int y = 0; y < phrase.length() - x; y++){
char n = phrase.charAt(y);
System.out.print(n+"-");
}
for (int a = 0; a < 3; a++){
System.out.print("."+"-");
}
System.out.println("");
}
for (int j = 0; j < 3; j++){
for (int i = 0; i < 3 - j; i++){
System.out.print("."+"-");
}
System.out.println("");
}
}
删除最后一个 -
的一种方法是仅在 不是最后一个迭代 时打印 -
。您可以通过检查 loopVariable != loopBound - 1
.
因此您的代码将是:
for (int x = 0; x < phrase.length(); x++){
for (int y = 0; y < phrase.length() - x; y++){
char n = phrase.charAt(y);
System.out.print(n+"-");
}
// You could just print the literal "--.-." instead
for (int a = 0; a < 3; a++){
System.out.print(".");
if (a != 2) { // Notice here!
System.out.print("-");
}
}
System.out.println();
}
for (int j = 0; j < 3; j++){
for (int i = 0; i < 3 - j; i++){
System.out.print(".");
if (i != 2 - j) { // and here
System.out.print("-");
}
}
System.out.println();
}
我会这样做:
// pretend that the phrase is 3 characters longer than it actually is...
// because of the three dots at the end
for (int x = 0; x < phrase.length() + 3; x++){
for (int y = 0; y < phrase.length() + 3 - x; y++){
char n;
// Should we print the phrase or the dots?
if (y < phrase.length() - x) {
n = phrase.charAt(y);
} else {
n = '.';
}
System.out.print(n);
if (y != phrase.length() + 2 - x) { // same trick of checking if it is last iteration
System.out.print("-");
}
}
System.out.println();
}