需求:编写一个从1循环到150并在每行打印一个值,另外在每个3的倍数行上打印出foo,在每个5的倍数行上打印biz,在每个7的倍数上打印baz.
package study01; public class For { public static void main(String[] args) { for(int i = 1;i<=150;i++){ System.out.print(i); if(i%3==0){ System.out.print(" foo"); } if(i%5==0){ System.out.print(" biz"); } if(i%7==0){ System.out.print(" baz"); } System.out.println(""); } } }
输出结果如下:
1
2
3 foo
4
5 biz
6 foo
7 baz
8
9 foo
10 biz
11
12 foo
13
14 baz
15 foo biz
16
17
18 foo
19
20 biz
21 foo baz
22
23
24 foo
25 biz
26
27 foo
28 baz
29
30 foo biz
31
32
33 foo
34
35 biz baz
36 foo
37
38
39 foo
40 biz
41
42 foo baz
43
44
45 foo biz
46
47
48 foo
49 baz
50 biz
51 foo
52
53
54 foo
55 biz
56 baz
57 foo
58
59
60 foo biz
61
62
63 foo baz
64
65 biz
66 foo
67
68
69 foo
70 biz baz
71
72 foo
73
74
75 foo biz
76
77 baz
78 foo
79
80 biz
81 foo
82
83
84 foo baz
85 biz
86
87 foo
88
89
90 foo biz
91 baz
92
93 foo
94
95 biz
96 foo
97
98 baz
99 foo
100 biz
101
102 foo
103
104
105 foo biz baz
106
107
108 foo
109
110 biz
111 foo
112 baz
113
114 foo
115 biz
116
117 foo
118
119 baz
120 foo biz
121
122
123 foo
124
125 biz
126 foo baz
127
128
129 foo
130 biz
131
132 foo
133 baz
134
135 foo biz
136
137
138 foo
139
140 biz baz
141 foo
142
143
144 foo
145 biz
146
147 foo baz
148
149
150 foo biz