How to print an additional * after every 5th and following 3rd number in a positive integer sequence?

huangapple go评论75阅读模式
英文:

How to print an additional * after every 5th and following 3rd number in a positive integer sequence?

问题

我正在尝试打印如下内容:

1, 2, 3, 4, 5, *, 6, 7, 8, *, 9, 10, 11, 12, 13, *, 14, 15, 16, *, 17, ...

其中,在每第5个和随后的第3个整数之后,都应该重复打印一个额外的 *

以下是我目前的代码:

for (int i = 1; i <= 100; i++) {
    if (i % 5 == 0) {
        System.out.print("* ");
    } else {
        System.out.print(i + " ");
    }
}

但这段代码打印出来的结果是:

1, 2, 3, 4, *, 6, 7, 8, 9, *, 11, 12, 13, 14, *, 16, 17, ...

我在这里做错了什么?

英文:

I'm trying to print

1, 2, 3, 4, 5, *, 6, 7, 8, *, 9, 10, 11, 12, 13, *, 14, 15, 16, *, 17, ...

where an additional * should be printed after every 5th and following 3rd integer repeatedly.

Here's my current code

for (int i = 1; i &lt;= 100; i++) {
    if (i % 5 == 0) {
        System.out.print(&quot;* &quot;);
    } else {
        System.out.print(i + &quot; &quot;);
    }
}

which prints

1, 2, 3, 4, *, 6, 7, 8, 9, *, 11, 12, 13, 14, *, 16, 17, ...

What am I doing wrong here?

答案1

得分: 1

这产生了预期的输出:

class Main {
  public static void main(String[] args) {
    for (int i = 1; i <= 100; i++) {
        System.out.print(i + " ");
        if ((i-5) % 8 == 0 || i % 8 == 0) {
            System.out.print("* ");
        }
    }
  }
}

// 输出:
// 1 2 3 4 5 * 6 7 8 * 9 10 11 12 13 * 14 15 16 * 17 18 19 20 21 * 22
// 23 24 * 25 26 27 28 29 * 30 31 32 * 33 34 35 36 37 * 38 39 40 * 41
// 42 43 44 45 * 46 47 48 * 49 50 51 52 53 * 54 55 56 * 57 58 59 60 61
// * 62 63 64 * 65 66 67 68 69 * 70 71 72 * 73 74 75 76 77 * 78 79 80
// * 81 82 83 84 85 * 86 87 88 * 89 90 91 92 93 * 94 95 96 * 97 98 99 100

Dry run 链接:https://repl.it/repls/MarriedPreemptiveLeadership

同时,感谢 @Rogue 提供的数学公式。

这是 @Rogue 提供的解释:

你有 5、8、13、16、21 等。因此,这些索引位于间隔为 5、5+3(8)或 5+(8n) 的位置。因此,你可以检查是否满足 ((i-5) % 8 == 0 || i % 8 == 0),如果满足,则在打印数字后在模式中打印一个星号。毕竟,数字总是被打印出来的。

英文:

This is giving the expected output:

class Main {
  public static void main(String[] args) {
    for (int i = 1; i &lt;= 100; i++) {
        System.out.print(i + &quot; &quot;);
        if ((i-5) % 8 == 0 || i % 8 == 0) {
            System.out.print(&quot;* &quot;);
        }
    }
  }
}

// Output: 
// 1 2 3 4 5 * 6 7 8 * 9 10 11 12 13 * 14 15 16 * 17 18 19 20 21 * 22
// 23 24 * 25 26 27 28 29 * 30 31 32 * 33 34 35 36 37 * 38 39 40 * 41
// 42 43 44 45 * 46 47 48 * 49 50 51 52 53 * 54 55 56 * 57 58 59 60 61
// * 62 63 64 * 65 66 67 68 69 * 70 71 72 * 73 74 75 76 77 * 78 79 80
// * 81 82 83 84 85 * 86 87 88 * 89 90 91 92 93 * 94 95 96 * 97 98 99 100

Dry run link: https://repl.it/repls/MarriedPreemptiveLeadership

Also, thanks to @Rogue for coming up with the mathematical formula.

Here's the explanation provided by @Rogue

> You have 5, 8, 13, 16, 21, etc. So it follows that these indices lie
> after a gap of 5, or 5+3 (8), or 5+(8n). So from there, you could
> check if ((i-5) % 8 == 0 || i % 8 == 0), and if so, print an asterisk
> in your pattern after printing your number. After all, the numbers are
> always printed.

答案2

得分: 0

我不能说如何使其显示您想要的内容,但它之所以在每个5(5、10、15等)处显示*,是因为是一个“模数”运算符,它在除法后获得余数。

因此,5%5的结果为== 0(因此打印出“*”)因为在将5 / 5相除后没有余数。同样,10 / 5也是如此,等等。6%5会给出余数1`。

英文:

I can't say how to make it show what you want, but the reason it's showing * at every 5 (5, 10, 15, etc.) is that % is a "modulus" operator which gets the remainder after dividing.

So 5 % 5 does == 0 (and thus prints out &quot;* &quot;) as there is no remainder after dividing 5 / 5. Same as 10 / 5, etc. 6 % 5 would give 1 as the remainder.

huangapple
  • 本文由 发表于 2020年6月29日 11:41:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/62630845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定