数酒瓶童谣:从 99 数到 0

发布于: 13 小时前
数酒瓶童谣:从99数到0

设计真正的应用程序

让我们将你已经学习到的Java技能发挥到真正有用的程序上。我们需要一个带有main()的类、一个int与

String变量、一个while循环和一个if测试。稍加整理你就能立即创建出有用的程序。但在你偷看示例程序

代码之前,先自己想想看你会怎样写一个程序从99数到0。下面是数啤酒瓶童谣的程序:

public class BeerSong {
public static void main (String [] args){
int beerNum = 99;
String word = "bottles";
while (beerNum > 0){
if(beerNum == 1){
word = "bottle";
}
System.out.println(beerNum + "" + word + " of beer on the wall");
System.out.println(beerNum + "" + word + " of beer.");
System.out.println("Take one down.");
System.out.println("Pass it around.");
beerNum = beerNum - 1;
if (beerNum > 0){
System.out.println(beerNum + "" + word + " of beer on the wall");
}else {
System.out.println("No more botlles of beer on the wall");
}
}//while循环结束
}//main方法循环结束
}//class 结束

运行结果:

99bottles of beer on the wall
99bottles of beer.
Take one down.
Pass it around.
98bottles of beer on the wall
98bottles of beer on the wall
98bottles of beer.
Take one down.
Pass it around.
97bottles of beer on the wall
97bottles of beer on the wall
97bottles of beer.
Take one down.
Pass it around.
96bottles of beer on the wall
96bottles of beer on the wall
96bottles of beer.
Take one down.
Pass it around.
95bottles of beer on the wall
95bottles of beer on the wall
95bottles of beer.
Take one down.
Pass it around.
94bottles of beer on the wall
94bottles of beer on the wall
94bottles of beer.
Take one down.
Pass it around.
93bottles of beer on the wall
93bottles of beer on the wall
93bottles of beer.
Take one down.
Pass it around.
92bottles of beer on the wall
92bottles of beer on the wall
92bottles of beer.
Take one down.
Pass it around.
91bottles of beer on the wall
91bottles of beer on the wall
91bottles of beer.
Take one down.
Pass it around.
90bottles of beer on the wall
90bottles of beer on the wall
90bottles of beer.
Take one down.
Pass it around.
89bottles of beer on the wall
89bottles of beer on the wall
89bottles of beer.
Take one down.
Pass it around.
88bottles of beer on the wall
88bottles of beer on the wall
88bottles of beer.
Take one down.
Pass it around.
87bottles of beer on the wall
87bottles of beer on the wall
87bottles of beer.
Take one down.
Pass it around.
86bottles of beer on the wall
86bottles of beer on the wall
86bottles of beer.
Take one down.
Pass it around.
85bottles of beer on the wall
85bottles of beer on the wall
85bottles of beer.
Take one down.
Pass it around.
84bottles of beer on the wall
84bottles of beer on the wall
84bottles of beer.
Take one down.
Pass it around.
83bottles of beer on the wall
83bottles of beer on the wall
83bottles of beer.
Take one down.
Pass it around.
82bottles of beer on the wall
82bottles of beer on the wall
82bottles of beer.
Take one down.
Pass it around.
81bottles of beer on the wall
81bottles of beer on the wall
81bottles of beer.
Take one down.
Pass it around.
80bottles of beer on the wall
80bottles of beer on the wall
80bottles of beer.
Take one down.
Pass it around.
79bottles of beer on the wall
79bottles of beer on the wall
79bottles of beer.
Take one down.
Pass it around.
78bottles of beer on the wall
78bottles of beer on the wall
78bottles of beer.
Take one down.
Pass it around.
77bottles of beer on the wall
77bottles of beer on the wall
77bottles of beer.
Take one down.
Pass it around.
76bottles of beer on the wall
76bottles of beer on the wall
76bottles of beer.
Take one down.
Pass it around.
75bottles of beer on the wall
75bottles of beer on the wall
75bottles of beer.
Take one down.
Pass it around.
74bottles of beer on the wall
74bottles of beer on the wall
74bottles of beer.
Take one down.
Pass it around.
73bottles of beer on the wall
73bottles of beer on the wall
73bottles of beer.
Take one down.
Pass it around.
72bottles of beer on the wall
72bottles of beer on the wall
72bottles of beer.
Take one down.
Pass it around.
71bottles of beer on the wall
71bottles of beer on the wall
71bottles of beer.
Take one down.
Pass it around.
70bottles of beer on the wall
70bottles of beer on the wall
70bottles of beer.
Take one down.
Pass it around.
69bottles of beer on the wall
69bottles of beer on the wall
69bottles of beer.
Take one down.
Pass it around.
68bottles of beer on the wall
68bottles of beer on the wall
68bottles of beer.
Take one down.
Pass it around.
67bottles of beer on the wall
67bottles of beer on the wall
67bottles of beer.
Take one down.
Pass it around.
66bottles of beer on the wall
66bottles of beer on the wall
66bottles of beer.
Take one down.
Pass it around.
65bottles of beer on the wall
65bottles of beer on the wall
65bottles of beer.
Take one down.
Pass it around.
64bottles of beer on the wall
64bottles of beer on the wall
64bottles of beer.
Take one down.
Pass it around.
63bottles of beer on the wall
63bottles of beer on the wall
63bottles of beer.
Take one down.
Pass it around.
62bottles of beer on the wall
62bottles of beer on the wall
62bottles of beer.
Take one down.
Pass it around.
61bottles of beer on the wall
61bottles of beer on the wall
61bottles of beer.
Take one down.
Pass it around.
60bottles of beer on the wall
60bottles of beer on the wall
60bottles of beer.
Take one down.
Pass it around.
59bottles of beer on the wall
59bottles of beer on the wall
59bottles of beer.
Take one down.
Pass it around.
58bottles of beer on the wall
58bottles of beer on the wall
58bottles of beer.
Take one down.
Pass it around.
57bottles of beer on the wall
57bottles of beer on the wall
57bottles of beer.
Take one down.
Pass it around.
56bottles of beer on the wall
56bottles of beer on the wall
56bottles of beer.
Take one down.
Pass it around.
55bottles of beer on the wall
55bottles of beer on the wall
55bottles of beer.
Take one down.
Pass it around.
54bottles of beer on the wall
54bottles of beer on the wall
54bottles of beer.
Take one down.
Pass it around.
53bottles of beer on the wall
53bottles of beer on the wall
53bottles of beer.
Take one down.
Pass it around.
52bottles of beer on the wall
52bottles of beer on the wall
52bottles of beer.
Take one down.
Pass it around.
51bottles of beer on the wall
51bottles of beer on the wall
51bottles of beer.
Take one down.
Pass it around.
50bottles of beer on the wall
50bottles of beer on the wall
50bottles of beer.
Take one down.
Pass it around.
49bottles of beer on the wall
49bottles of beer on the wall
49bottles of beer.
Take one down.
Pass it around.
48bottles of beer on the wall
48bottles of beer on the wall
48bottles of beer.
Take one down.
Pass it around.
47bottles of beer on the wall
47bottles of beer on the wall
47bottles of beer.
Take one down.
Pass it around.
46bottles of beer on the wall
46bottles of beer on the wall
46bottles of beer.
Take one down.
Pass it around.
45bottles of beer on the wall
45bottles of beer on the wall
45bottles of beer.
Take one down.
Pass it around.
44bottles of beer on the wall
44bottles of beer on the wall
44bottles of beer.
Take one down.
Pass it around.
43bottles of beer on the wall
43bottles of beer on the wall
43bottles of beer.
Take one down.
Pass it around.
42bottles of beer on the wall
42bottles of beer on the wall
42bottles of beer.
Take one down.
Pass it around.
41bottles of beer on the wall
41bottles of beer on the wall
41bottles of beer.
Take one down.
Pass it around.
40bottles of beer on the wall
40bottles of beer on the wall
40bottles of beer.
Take one down.
Pass it around.
39bottles of beer on the wall
39bottles of beer on the wall
39bottles of beer.
Take one down.
Pass it around.
38bottles of beer on the wall
38bottles of beer on the wall
38bottles of beer.
Take one down.
Pass it around.
37bottles of beer on the wall
37bottles of beer on the wall
37bottles of beer.
Take one down.
Pass it around.
36bottles of beer on the wall
36bottles of beer on the wall
36bottles of beer.
Take one down.
Pass it around.
35bottles of beer on the wall
35bottles of beer on the wall
35bottles of beer.
Take one down.
Pass it around.
34bottles of beer on the wall
34bottles of beer on the wall
34bottles of beer.
Take one down.
Pass it around.
33bottles of beer on the wall
33bottles of beer on the wall
33bottles of beer.
Take one down.
Pass it around.
32bottles of beer on the wall
32bottles of beer on the wall
32bottles of beer.
Take one down.
Pass it around.
31bottles of beer on the wall
31bottles of beer on the wall
31bottles of beer.
Take one down.
Pass it around.
30bottles of beer on the wall
30bottles of beer on the wall
30bottles of beer.
Take one down.
Pass it around.
29bottles of beer on the wall
29bottles of beer on the wall
29bottles of beer.
Take one down.
Pass it around.
28bottles of beer on the wall
28bottles of beer on the wall
28bottles of beer.
Take one down.
Pass it around.
27bottles of beer on the wall
27bottles of beer on the wall
27bottles of beer.
Take one down.
Pass it around.
26bottles of beer on the wall
26bottles of beer on the wall
26bottles of beer.
Take one down.
Pass it around.
25bottles of beer on the wall
25bottles of beer on the wall
25bottles of beer.
Take one down.
Pass it around.
24bottles of beer on the wall
24bottles of beer on the wall
24bottles of beer.
Take one down.
Pass it around.
23bottles of beer on the wall
23bottles of beer on the wall
23bottles of beer.
Take one down.
Pass it around.
22bottles of beer on the wall
22bottles of beer on the wall
22bottles of beer.
Take one down.
Pass it around.
21bottles of beer on the wall
21bottles of beer on the wall
21bottles of beer.
Take one down.
Pass it around.
20bottles of beer on the wall
20bottles of beer on the wall
20bottles of beer.
Take one down.
Pass it around.
19bottles of beer on the wall
19bottles of beer on the wall
19bottles of beer.
Take one down.
Pass it around.
18bottles of beer on the wall
18bottles of beer on the wall
18bottles of beer.
Take one down.
Pass it around.
17bottles of beer on the wall
17bottles of beer on the wall
17bottles of beer.
Take one down.
Pass it around.
16bottles of beer on the wall
16bottles of beer on the wall
16bottles of beer.
Take one down.
Pass it around.
15bottles of beer on the wall
15bottles of beer on the wall
15bottles of beer.
Take one down.
Pass it around.
14bottles of beer on the wall
14bottles of beer on the wall
14bottles of beer.
Take one down.
Pass it around.
13bottles of beer on the wall
13bottles of beer on the wall
13bottles of beer.
Take one down.
Pass it around.
12bottles of beer on the wall
12bottles of beer on the wall
12bottles of beer.
Take one down.
Pass it around.
11bottles of beer on the wall
11bottles of beer on the wall
11bottles of beer.
Take one down.
Pass it around.
10bottles of beer on the wall
10bottles of beer on the wall
10bottles of beer.
Take one down.
Pass it around.
9bottles of beer on the wall
9bottles of beer on the wall
9bottles of beer.
Take one down.
Pass it around.
8bottles of beer on the wall
8bottles of beer on the wall
8bottles of beer.
Take one down.
Pass it around.
7bottles of beer on the wall
7bottles of beer on the wall
7bottles of beer.
Take one down.
Pass it around.
6bottles of beer on the wall
6bottles of beer on the wall
6bottles of beer.
Take one down.
Pass it around.
5bottles of beer on the wall
5bottles of beer on the wall
5bottles of beer.
Take one down.
Pass it around.
4bottles of beer on the wall
4bottles of beer on the wall
4bottles of beer.
Take one down.
Pass it around.
3bottles of beer on the wall
3bottles of beer on the wall
3bottles of beer.
Take one down.
Pass it around.
2bottles of beer on the wall
2bottles of beer on the wall
2bottles of beer.
Take one down.
Pass it around.
1bottles of beer on the wall
1bottle of beer on the wall
1bottle of beer.
Take one down.
Pass it around.
No more botlles of beer on the wall
Process finished with exit code 0

虽然程序可以编译运行,但输出有点不美观,你可以试试看改漂亮一点。

用户头像

程李文华

关注

还未添加个人签名 2019.10.07 加入

还未添加个人简介

评论

发布
暂无评论
数酒瓶童谣:从99数到0