如何在java中使用if-else语句进行do ... while循环?

时间:2014-10-15 22:15:27

标签: java loops

我得到了这个任务:

“编写一个程序(Blueberry.java),它使用while循环打印整数1到100,每行一个数字,并将数字的字段宽度设置为3.当一个数字可被3整除时,打印”蓝色“在同一行上,并用空格分隔。按照相同的格式,当一个数字可被5整除时,打印”Berry“;当一个数字可被15整除时,打印”Blueberry“。注意一个数字可被15整除也可以被3和5整除。在这种情况下,只应打印“蓝莓”。应在第一行打印一条消息“以下是来自一个循环”。三条消息,一条打开应该打印每一行,分别报告“Blue”,“Berry”和“Blueberry”的总数。最后,在同一个程序中,添加一个for循环和一个do ... while循环:每个完成相同的任务while循环。“ 我已经找到了while循环和for循环,但是我遇到了do ... while循环的问题。

这是我到目前为止所做的:

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:

以下是来自while循环:

 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. 

以下内容来自for循环:

 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. 

以下是来自do ... while循环: 101

do ... while循环我做错了什么?

3 个答案:

答案 0 :(得分:2)

在开始variable之前,您应初始化int i = 1 do..while loop

这就是为什么你从[{1}} 101获得i = 101 for的原因。

答案 1 :(得分:0)

一个问题可能是你没有在while循环之后将你的'i'计数器设置为0,所以这对do {} while循环开始为100,因为条件是一次然后检查是否'i '等于或大于100才能停止。

尝试在while循环后将i设置为0

我希望它有所帮助! :)

答案 2 :(得分:0)

do-while循环将至少使用1次,然后开始比较... 那与the while和for的区别,也许这就是你的问题

并为您的代码。你没有初始化&#39;我&#39;变量

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

我希望这可以帮助你