简体   繁体   English

可被5或6整除

[英]Divisible by 5 or 6

I am trying to make a program where it lists all the numbers between 100-1000 that are divisible by 5 or 6. This is the code I used: 我正在尝试制作一个程序,其中列出了100-1000之间可以被5或6整除的所有数字。这是我使用的代码:

public class divisibleBy5and6 {
    public static void main (String[] args) {

        j = 1; 
        int number = 100;
        while (number < 1001) {
            if (number % 6 == 0 || number % 5 == 0) {
                System.out.print(number + ", ");
                number++;
                j++; }

            if (j % 10 == 0 && j != 0) {
                System.out.println();
                j++; }
            else {
                number++;
            }
        }

I used int j to make it so there are 9 per row. 我用int j做到了,所以每行有9个。 This is my output: 这是我的输出:

100, 102, 105, 108, 110, 114, 120, 125, 130, 
132, 135, 138, 140, 144, 150, 155, 160, 162, 
165, 168, 170, 174, 180, 185, 190, 192, 195, 
198, 200, 204, 210, 215, 220, 222, 225, 228, 
230, 234, 240, 245, 250, 252, 255, 258, 260, 
264, 270, 275, 280, 282, 285, 288, 290, 294, 
295, 300, 305, 310, 312, 315, 318, 320, 324, 
325, 330, 335, 340, 342, 345, 348, 350, 354, 
355, 360, 365, 370, 372, 375, 378, 380, 384, 
385, 390, 395, 400, 402, 405, 408, 410, 414, 
415, 420, 425, 430, 432, 435, 438, 440, 444, 
445, 450, 455, 460, 462, 465, 468, 470, 474, 
475, 480, 485, 490, 492, 495, 498, 500, 504, 
505, 510, 515, 520, 522, 525, 528, 530, 534, 
535, 540, 545, 550, 552, 555, 558, 560, 564, 
565, 570, 575, 580, 582, 585, 588, 590, 594, 
595, 600, 605, 610, 612, 615, 618, 620, 624, 
625, 630, 635, 640, 642, 645, 648, 650, 654, 
655, 660, 665, 670, 672, 675, 678, 680, 684, 
685, 690, 695, 700, 702, 705, 708, 710, 714, 
715, 720, 725, 730, 732, 735, 738, 740, 744, 
745, 750, 755, 760, 762, 765, 768, 770, 774, 
775, 780, 785, 790, 792, 795, 798, 800, 804, 
805, 810, 815, 820, 822, 825, 828, 830, 834, 
835, 840, 845, 850, 852, 855, 858, 860, 864, 
865, 870, 875, 880, 882, 885, 888, 890, 894, 
895, 900, 905, 910, 912, 915, 918, 920, 924, 
925, 930, 935, 940, 942, 945, 948, 950, 954, 
955, 960, 965, 970, 972, 975, 978, 980, 984, 
985, 990, 995, 1000, 

This obviously isn't right because I am missing numbers like 115 . 这显然是不对的,因为我缺少115数字。 What am I doing wrong? 我究竟做错了什么?

You need to seperate the number incrementation from the if/else statements. 您需要将数字增量与if / else语句分开。 It should happen at each iteration no matter what... 无论何时,它都应该在每次迭代中发生...

int j = 1; 
int number = 100;
while (number < 1001) {
  if (number % 6 == 0 || number % 5 == 0) {
    System.out.print(number + ", ");
    j++; 
   }
   if (j % 10 == 0) {
     System.out.println();
   }
   number++;
}

The problem is that sometimes, you're incrementing number twice in each loop. 问题是有时您会在每个循环中将number递增两次。 The first time is if the number is a multiple of 6 or of 5 , and the second time is when you don't print a newline. 第一次是数字是65的倍数,第二次是您不打印换行符。 This has the effect of skipping numbers, such that consecutive multiples of 5 or 6 won't print, such as your 115 , because it's after 114 and is skipped. 这具有跳过数字的效果,因此不会打印56连续倍数,例如115 ,因为它在114之后并且被跳过。 Notice also that other consecutive multiples of 5 or 6 won't print, such as the 126 after the 125 . 通知还的其它连续倍数56将不打印,如126的后125

Make it clear that you increment number only once per loop but removing number++; 要清楚,你增加number每循环,但只取出一次number++; from the if and else blocks, and unconditionally increment by placing number++; ifelse块开始,并通过放置number++;无条件地递增number++; at the very bottom of the while loop. while循环的最底部。

remove the first place it says number++; 删除显示number++;的第一位number++; after the System.out.print(number + ", "); System.out.print(number + ", ");

alter 改变

else {
                number++;
            }

It does this when you've just printed a newline. 当您刚刚打印换行符时,它将执行此操作。

to

            number++;

It will do this on every loop iteration number should be incremented non conditionally 它将在每个循环迭代number上执行此操作,该迭代number应无条件地增加

Try this: 尝试这个:

public class DivisibleBy5and6 {
    public static void main (String[] args) {

        int j = 1; 
        for (int number = 100 ; number < 1001; number++) {
            if (number % 6 == 0 || number % 5 == 0) {
                System.out.print(number + ", ");
                j++; 
            }

            if (j % 10 == 0) {// j never equals 0
                System.out.println();
                j++; 
            }
        }
    }
}

BTW, For definite loops it's better to use for instead of while . 顺便说一句,对于定环,最好使用for而不是while (makes your code more readable) (使您的代码更具可读性)

As you see you don't get confused this way and increase number once and in the right place. 如您所见,您不会为此感到困惑,而是在正确的位置一次增加number

You had a duplicate number++ which was executed twice when both conditions were true. 您有一个重复的number++ ,当两个条件都为真时,该number++将执行两次。 That overjumps some values: now corrected: 这会超出一些值:现在已更正:

int j = 1;
int number = 100;
while (number < 1001) {
    if (number % 6 == 0 || number % 5 == 0) {
        System.out.print(number + ", ");
        j++;
    }

    if (j % 10 == 0 && j != 0) {
        System.out.println();
        j++;
    }
    number++;
}

Your problem is that the code is incorrectly formatted. 您的问题是代码格式错误。 Properly indent it and it becomes much clearer: 适当地缩进它,它将变得更加清晰:

public class divisibleBy5and6 {
    public static void main (String[] args) {

        j = 1; 
        int number = 100;
        while (number < 1001) {
            if (number % 6 == 0 || number % 5 == 0) {
                System.out.print(number + ", ");
                number++;
                j++;
            }

            if (j % 10 == 0 && j != 0) {
                System.out.println();
                j++;
            }
            else {
                number++;
            }
        }

Then you can see that the else is attached to the second if , not the first, so the incrementing of number can occur twice. 然后,您可以看到else附加到第二个if ,而不是第一个,因此number的递增可以出现两次。

After that it's clearer that, inside the first if , number is incremented ONLY if the number is printed. 之后,很明显,在第一个if ,仅在打印了数字的ifnumber递增。 What you really want is for number to be incremented for every iteration, and the best way to assure that is to ONLY increment number immediately prior to the closing } of the while . 你真正想要的是number将递增每次迭代,并保证最好的办法是只增加数之前立即关闭}中的while

Then you'll realize that the while(number < 1001) ... number++ loop can be replaced with a more robust for (int number 100; number < 1001; number++) , totally removing the incrementing of number from the loop body. 然后您将意识到, while(number < 1001) ... number++循环可以用更强大的for (int number 100; number < 1001; number++)代替,从而完全从循环体中删除了number的增量。

When designing a loop it's quite important to keep straight which values are "invariant" in the loop (never change at all), which are simply incremented with each iteration, and which are modified on a more irregular basis. 在设计循环时,保持循环中哪些值是“不变的”(根本不改变)是非常重要的,这些值随每次迭代简单地增加,并且在不规则的基础上进行修改。 If you keep these concepts straight then loops are much less intimidating. 如果您直截了当地讲这些概念,那么循环就不会那么令人生畏。

public class pattern {
    public static void main (String[] args) {

        int j = 0; 
        int number = 100;
        while (number < 1001) {
            if (number % 6 == 0 || number % 5 == 0) {
                System.out.print(number + ", ");
                j++; }

            if (j % 10 == 0&&(number % 6 == 0 || number % 5 == 0)) {
                System.out.println();
                number++;     
            }
            else
            {
                number++;
            }
        }
    }
}

Try this code. 试试这个代码。

The mistake in your code is that you are actually incrementing number twice so that's why you are missing some numbers. 您的代码中的错误是您实际上是将number递增两次,所以这就是为什么您缺少一些数字的原因。 Good luck :) 祝好运 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM