• Post Reply Bookmark Topic Watch Topic
  • New Topic

Not printing the output I want using While loops?  RSS feed

 
Zukky Bee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm new to this forum and relatively new to Java, so forgive me if i'm a real noob. But anyway, I am learning while loops but the output I am getting is not what I want? My class:



The output I am getting is this:
99 bottles on the wall!
98 bottles on the wall!
97 bottles on the wall!
96 bottles on the wall!
95 bottles on the wall!
94 bottles on the wall!
93 bottles on the wall!
92 bottles on the wall!
91 bottles on the wall!
90 bottles on the wall!
89 bottles on the wall!
88 bottles on the wall!
87 bottles on the wall!
86 bottles on the wall!
85 bottles on the wall!
84 bottles on the wall!
83 bottles on the wall!
82 bottles on the wall!
81 bottles on the wall!
80 bottles on the wall!
79 bottles on the wall!
78 bottles on the wall!
77 bottles on the wall!
76 bottles on the wall!
75 bottles on the wall!
74 bottles on the wall!
73 bottles on the wall!
72 bottles on the wall!
71 bottles on the wall!
70 bottles on the wall!
69 bottles on the wall!
68 bottles on the wall!
67 bottles on the wall!
66 bottles on the wall!
65 bottles on the wall!
64 bottles on the wall!
63 bottles on the wall!
62 bottles on the wall!
61 bottles on the wall!
60 bottles on the wall!
59 bottles on the wall!
58 bottles on the wall!
57 bottles on the wall!
56 bottles on the wall!
55 bottles on the wall!
54 bottles on the wall!
53 bottles on the wall!
52 bottles on the wall!
51 bottles on the wall!
50 bottles on the wall!
49 bottles on the wall!
48 bottles on the wall!
47 bottles on the wall!
46 bottles on the wall!
45 bottles on the wall!
44 bottles on the wall!
43 bottles on the wall!
42 bottles on the wall!
41 bottles on the wall!
40 bottles on the wall!
39 bottles on the wall!
38 bottles on the wall!
37 bottles on the wall!
36 bottles on the wall!
35 bottles on the wall!
34 bottles on the wall!
33 bottles on the wall!
32 bottles on the wall!
31 bottles on the wall!
30 bottles on the wall!
29 bottles on the wall!
28 bottles on the wall!
27 bottles on the wall!
26 bottles on the wall!
25 bottles on the wall!
24 bottles on the wall!
23 bottles on the wall!
22 bottles on the wall!
21 bottles on the wall!
20 bottles on the wall!
19 bottles on the wall!
18 bottles on the wall!
17 bottles on the wall!
16 bottles on the wall!
15 bottles on the wall!
14 bottles on the wall!
13 bottles on the wall!
12 bottles on the wall!
11 bottles on the wall!
10 bottles on the wall!
9 bottles on the wall!
8 bottles on the wall!
7 bottles on the wall!
6 bottles on the wall!
5 bottles on the wall!
4 bottles on the wall!
3 bottles on the wall!
2 bottles on the wall!
1 bottles on the wall!
Only 1 bottle left on the wall!

I want it to stop on "2 bottles on the wall!" and then print "Only 1 bottle left on the wall!". How do I go about doing that? I have tried different variations of the condition (x>1), I have tried (x>5) but it still goes down to 1 bottle?

Thank you.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

I can think of two approaches. One is to add an extra if statement (for the case where x is not 1). The other is to move the if statement outside the while loop.

These are meant to be hints. Do they help give you any ideas?
 
Zukky Bee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Welcome to CodeRanch!

I can think of two approaches. One is to add an extra if statement (for the case where x is not 1). The other is to move the if statement outside the while loop.

These are meant to be hints. Do they help give you any ideas?


I put the if statement outside and it worked . Also, I kept on compiling from the wrong .class, so no matter what I did, the output would not change lol. My mistake. But thank you for your help .
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad it is working now. And that you learned something new about compiling .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!