简体   繁体   中英

How to make a do…while loop with an if-else statement in java?

I was given this assignment:

"Write a program (Blueberry.java) that uses a while loop to print the integers 1 to 100, one number on each line, and set the number's field width as 3. When a number is divisible by 3, print “Blue” on the same line, and separate them by a space. Follow the same format, when a number is divisible by 5, print “Berry”; and when a number is divisible by 15, print “Blueberry”. Note that a number divisible by 15 will also be divisible by 3 and 5. In this case, only “Blueberry” should be printed. A message, “The following is from a while loop”, should be printed on the first line. Three messages, one on each line, should be printed to report the total numbers of “Blue”, “Berry”, and “Blueberry” respectively. Finally, in the same program, add a for loop and a do…while loop: each accomplishes the same task as the while loop." I've figured out the while loop and the for loop, but I'm having trouble with the do...while loop.

This is what I have so far:

import java.util.Scanner;

public class Blueberry {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("The following is from a while loop: ");
        int blueCount = 0;
        int berryCount = 0;
        int blueberryCount = 0;

        int i = 1; 
        while(i <= 100){
            if(i % 3 == 0 && i % 5 == 0) {
                System.out.printf("%3d %s\n",  i, "Blueberry");    
                blueberryCount++;
            }
            else if(i % 3 == 0) {
                System.out.printf("%3d %s\n",  i, "Blue");    
                blueCount++;
            }
            else if(i % 5 == 0) {
                System.out.printf("%3d %s\n",  i, "Berry");    
                berryCount++;
            }
            else {
            System.out.printf("%3d\n", i);
            }
            i++;
        }
        System.out.printf("There are %d Blues. \n",  blueCount);
        System.out.printf("There are %d Berries. \n",  berryCount);
        System.out.printf("There are %d Blueberries. \n",  blueberryCount);
        System.out.println();

        blueCount = 0;
        berryCount = 0;
        blueberryCount = 0;

        System.out.println("The following is from a for loop: ");
        for (i = 1; i <= 100; i++) {
            if(i % 3 == 0 && i % 5 == 0) {
                System.out.printf("%3d %s\n",  i, "Blueberry");
                blueberryCount++;
            }
            else if(i % 3 == 0) {
                System.out.printf("%3d %s\n",  i, "Blue");
                blueCount++;
            }
            else if(i % 5 == 0) {
                System.out.printf("%3d %s\n",  i, "Berry"); 
                berryCount++;
            }
            else {
            System.out.printf("%3d\n", i);

                }
            }
            System.out.printf("There are %d Blues. \n",  blueCount);
            System.out.printf("There are %d Berries. \n",  berryCount);
            System.out.printf("There are %d Blueberries. \n",  blueberryCount);
            System.out.println();

            blueCount = 0;
            berryCount = 0;
            blueberryCount = 0;

            System.out.println("The following is from a do...while loop: ");
            do {
                if(i % 3 == 0 && i % 5 == 0) {
                    System.out.printf("%3d %s\n",  i, "Blueberry");    
                    blueberryCount++;
                }
                else if(i % 3 == 0) {
                    System.out.printf("%3d %s\n",  i, "Blue");    
                    blueCount++;
                }
                else if(i % 5 == 0) {
                    System.out.printf("%3d %s\n",  i, "Berry");    
                    berryCount++;
                }
                else {
                System.out.printf("%3d\n", i);
                }
                i++;
                }

            while (i <= 100);
    }
}








and this is what it looks like when I run it:

The following is from a while loop:

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

The following is from a for loop:

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

The following is from a do...while loop: 101

What am I doing wrong with the do...while loop?

You should initialize variable int i = 1 before starting do..while loop .

That's why you get 101 because i = 101 from the previouse for loop .

well one problem may be that you are not setting your 'i' counter to 0 after the while loop, so this starts at 100 for the do{}while loop and since the condition is do once and then check if 'i' is equals or greater than 100 to stop.

Try setting i to 0 after the while loop

I hope it helps! :)

do-while loop is going to be used at least 1 time then the comparison will start... that's the difference with while and for, maybe that is your problem

and for your code. your are not initializing 'i' variable.

reference: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

i hope this helps you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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