【问题标题】:How to output a hyphen after each character with restrictions using nested for loops in java如何在Java中使用嵌套for循环在每个字符后输出一个连字符
【发布时间】:2020-09-16 00:55:46
【问题描述】:

我只是有一个小问题,在每个字符后输出一个连字符(包括下面代码中显示的三个点)

示例输入

  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("");
            }
        }

【问题讨论】:

  • 你需要使用循环吗?否则获取 char 数组然后加入它会更简单,编写的代码更少
  • 是的,只使用for循环,没有数组,或者java的任何内置方法
  • @HussainOmer "any" 内置方法将包括 System.out.printlnString.charAtString.length 等,所有这些你都使用过。我想更好的说法是“不允许我在我的代码中没有使用的方法”?

标签: java loops for-loop nested-loops


【解决方案1】:

删除最后一个- 的一种方法是仅在- 不是最后一次迭代时打印它。您可以通过检查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();
}

【讨论】:

  • 有没有办法减少连字符?我要做的就是删除一个连字符
  • @HussainOmer 我的意思是,你可以打印一个\b 字符来删除你打印的最后一个字符,但我觉得这违背了这个(也许是作业?)练习的精神......
  • 是的,好的,我尝试了你的方法,我将使用我拥有的当前更新的代码编辑我的代码并向你展示我的实际输出
  • @HussainOmer 我刚刚意识到一个错误。见你编辑。
  • 2 是循环边界,3,减 1。2 - j 是循环边界,3 - j,减 1。注意 3 - j - 1 == 2 - j。 @HussainOmer
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2014-04-02
  • 2014-06-29
  • 1970-01-01
  • 2022-11-01
  • 2020-01-10
相关资源
最近更新 更多