如何在java中使用if-else语句进行do…while循环?
我被分配了这个任务: 编写一个程序(Blueberry.java),使用while循环打印整数1到100,每行一个数字,并将数字的字段宽度设置为3。当一个数字可被3整除时,在同一行打印“Blue”,并用空格分隔。使用相同的格式,当一个数字可被5整除时,打印“Berry”;当一个数字可以被15整除时,打印“Blueberry”。请注意,一个可以被15整除的数字也可以被3和5整除。在这种情况下,只应打印“Blueberry”。一条消息,“以下内容来自while循环”,应打印在第一行。应打印三条消息,每行一条,分别报告“Blue”、“Berry”和“Blueberry”的总数。最后,在同一程序中,添加一个for循环和一个do…while循环:每个消息完成与while循环相同的任务。” 我已经计算出while循环和for循环,但是我在do…while循环中遇到了问题 这就是我到目前为止所做的:如何在java中使用if-else语句进行do…while循环?,java,loops,Java,Loops,我被分配了这个任务: 编写一个程序(Blueberry.java),使用while循环打印整数1到100,每行一个数字,并将数字的字段宽度设置为3。当一个数字可被3整除时,在同一行打印“Blue”,并用空格分隔。使用相同的格式,当一个数字可被5整除时,打印“Berry”;当一个数字可以被15整除时,打印“Blueberry”。请注意,一个可以被15整除的数字也可以被3和5整除。在这种情况下,只应打印“Blueberry”。一条消息,“以下内容来自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:
以下内容来自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循环中做错了什么?一个问题可能是,在while循环之后,您没有将'I'计数器设置为0,因此do{}while循环从100开始,因为条件是do once,然后检查'I'是否等于或大于100,以停止 尝试在while循环后将i设置为0
我希望有帮助!:) 在启动
do..while循环之前,您应该初始化变量int i=1
这就是为什么您会得到101
,因为i=101
来自前面的for
循环
当循环至少要使用一次时,比较就会开始。。。
这就是while和for的区别,也许这就是你的问题
和你的代码。您没有初始化“i”变量
参考:
我希望这对您有所帮助您在启动do..while
循环之前没有设置i
。这就是为什么它从101开始,这是前面一个循环的剩余部分。这是一个可怕的if-else语句。为什么不试试这个打印输出i,如果i%3是0打印蓝色,inc蓝莓计数,如果i%5是0打印浆果和inc蓝莓计数,如果i%3 inc蓝莓计数
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.