• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Newby needs help with a problem (no error) regrading for loops or if else

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

This is my very first post so don't mind my stupidity
I'm also a pure beginner in Java.

Anyways I'm having trouble with the code listed below, to tell you the truth I'm not sure where the program is located.
The output of program is suppose to be:

Ooh! Loook its a cat!
Cat: meow

Would you like to kick the cat?
<input here>

so if you type in yes
the cat will infinitely meow at you
if you type in no
the cat will purr at you 5 times.





The problem is that if you type in no nothing happens. did I made a mistake in the for loop? or is the ifs?
Please help.
Thanks in advance.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello and welcome to the Ranch!

As for your question, please have a look here:


a standard for loop has 3 parts:



So for your loop you have:

initial condition: int i set equal to 0 -- No problem here.

test condition: repeat while i is greater than 5 -- do you detect any problem with logic here?

do once each time we loop: increase i by one -- no problem here.

Good luck and hope this helps!
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is in for loop of the pur() method.



In the very first run of the for loop, i is 0. You are verifying if i is greater than 5, which if false. So, the code in the loop is never executed.

 
Timothy Onggowasito
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow! I knew it was the for loops!
Thanks for your help, both of you.
Now I can finally get on with a new one.
 
Sheriff
Posts: 7135
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Anoo Kota", please check your private messages regarding an important administrative matter from JavaRanch.
 
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic