Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Working code But not intended result?

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    15
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Working code But not intended result?

    Hello, again, I have another question if your able to help me. I am doing the Stanford 106a class on you tube so I am using acm imports which are outdated apparently but any help none the less would be appreciated.

    I am trying to write a program that will type out the song 99 bottles of beer on the wall counting all the way down to 0 where I declare I am drunk. The problem I am having is that when it runs what ever the user inputs as the value they want to start with it wont count backwards it will just use that number however many times it prints out(what ever is declared to start).

    I hope that explanation is good enough! Trying to be short sorry if not clear.Here is my code:

     
    		public void run() {
    			println("This program will sing the dreaded song 99 bottles of beer on the wall");
    			println("Feel free to change 99 to what ever you see fit");
    			int nBottles = readInt("Enter the perfered # of bottles you would like to start with");
    			println(+nBottles+"bottles of beer on the wall,");
    			for(int i=nBottles;i>0;i--) {
    				println(+nBottles+"bottles of beer on the wall");
    				println	(+nBottles+"bottles of beer.");
    				println	("you take one down and pass it around.");
     
     
    			}
     
     
    			println("now I drunk am...oh shit I got my mixed all words up and ");
    			println("I don't undermean what I stand...hiccup");


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Working code But not intended result?

    Look closely at your code to see what variable you print out. Does it change?
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    15
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Working code But not intended result?

    The variable nBottles is the one printed out but it doesn't change, it just prints that same number over until done instead of subtracting a number each time which is what I would like it to do. I think I am close, very tough trouble shooting myself though, the one bad side of self learning I guess.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Working code But not intended result?

    Surely you answered your own question. You have one variable that doesn't change which you print out and wonder why it prints the same value all the time. Then you have a second variable which does change. Hmmmm!
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Aug 2014
    Posts
    15
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Working code But not intended result?

    well I was looking for honest feed back not wise ass remarks, I am new to this like I said so no I didn't answer my own question, if I did I wouldn't know it(hence being new! HMMMM)

    The post also only involved one variable and I only asked about one, I got it to work with out the for loop

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Working code But not intended result?

    My smart ass comments are targetted at getting you to think and work it out for yourself . Rather than me just spoonfeed you the answer. If you do not appreciate my help, no probs I have better things to do than wipe your butt for you!
    Improving the world one idiot at a time!

  7. #7
    Junior Member
    Join Date
    Aug 2014
    Posts
    15
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Working code But not intended result?

    This is what I ended up using..
     
    		public void run() {
    			println("This program will sing the dreaded song 99 bottles of beer on the wall");
    			println("Feel free to change 99 to what ever you see fit");
    			int nBottles = readInt("Enter the perfered # of bottles you would like to start with");
    			println(+nBottles+" bottles of beer on the wall"+nBottles+" bottles of beer.");
    			println("you take on down and pass it around");
    			nBottles--;
     
    			while(nBottles>0) {
    				println(+nBottles+" bottles of beer on the wall.");
    				println(+nBottles+" bottles of beer on the wall,"+nBottles+" bottles of beer.");
    				println("you take one down and pass it around,");
    				nBottles--;
    			}
     
     
    			println("now I drunk am...oh shit I got my mixed all words up and ");
    			println("I don't undermean what I stand...hiccup");
     
     
    		}

    Is it possible for me to have done that program with a for loop?
    To me this almost made sense but I am struggling with 4 loops for some reason.

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Working code But not intended result?

    Quote Originally Posted by stanfordjava View Post
    I got it to work with out the for loop
    How? Several hundred print statements.

    Ahh while loop instead. Which basically does the same thing.
    Improving the world one idiot at a time!

  9. #9
    Junior Member
    Join Date
    Aug 2014
    Posts
    15
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Working code But not intended result?

    I see it does basically the same thing, I just don't understand why it wouldn't work for me with a for loop .So if I wrote it correctly it should still work with the for loop?
    Gonna have to try to practice

    --- Update ---

    I just got it to work with the for loop!! I just rearranged a few println's and added nBottles-- to inside the statement of the for loop.

    I do thank you Junky, I was grumpy just coming home from work and I found your post insulting to me is all, I realize your intentions and the fact that your on here helping ppl speaks for itself. thanks again and sry!

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Working code But not intended result?

    Quote Originally Posted by Junky View Post
    You have one variable that doesn't change which you print out and wonder why it prints the same value all the time. Then you have a second variable which does change.
    I told you what the problem was. Once again the variable you display on the screen is not changing value. The variable that changes is NOT being displayed on the screen. You can fix it in 2 ways.
    Improving the world one idiot at a time!

  11. #11
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Working code But not intended result?

    Take a look at what you wrote here:

    nBottles--;
    Then think about what Junky was explaining about 'how a variable changes'
    in the aspect of your original for loop algorithm. Use that theory in your original code
    and you will get the same output. Hint: 'what do you use to count with'.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

Similar Threads

  1. How to compile java code on server side and then send back result to client.
    By Sahil Arora in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 13th, 2013, 12:52 PM
  2. How do you code to compare result?
    By shin777 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 11th, 2013, 09:09 AM
  3. Replies: 15
    Last Post: December 1st, 2012, 10:13 PM
  4. Beginner trying to write Java code, has issue w/ printing result and 2 decimals
    By flpanthers1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2011, 11:11 AM