• 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

What is this code missing?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain where I am falling short in the following code? I want the records to print only once but either I get each record 3 times or the same result for all three recs.
[ November 12, 2002: Message edited by: Shirl Diva ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that the code you posted won't compile as the variable result was not properly initialized.
After changing the code to initialize result to 0, the code does compile and when run displays
What exactly did you expect the code to actually do and why? What's your best guess as to why it does what it actually does?
 
Shirl Diva
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want it to write the record once. My best guess is that I have it in a loop that performs 3 times. My problem is that I have tried it without the loop but I only get one record. Sorry about the result field. I had been moving the code around - seeing what difference would be. I attempted to take the write statement out of the loop - but the result field was a local variable and prevented it from working that way. Would it help any, or be wise for me to, make the result field global and work on it that way then take the write out of the loop??
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Shirl,
It can be confusing at first, but you'll get the hang of it! You are calculating three results, but because your print statements are after the loop, you only see the result from the last calculation. You could have three separate result variables, or you you could print each result inside the loop, before you compute a new one.
As far as style goes, there's no reason to pass thirdSide into your hypoteneuse method. Use a local variable instead. You should pass in the bothSides variable though. The hypoteneuse method uses that for its calculation, so it is better to have it as a parameter than a member variable. In fact, the best style would be to pass in the two values individually and let the hypoteneuse method do the full calculation, but I'm not sure what your assignment was.
 
Shirl Diva
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much Greg. At one point I had the print stmt in the loop, but I got nine recs - 3 of each. That's why I tried it on the outside. I am going with the print each rec prior to calc the next rec. I had thought about doing that before but feared that it would make the pgm to wordy. Thanx again. I think that I am cool now
 
reply
    Bookmark Topic Watch Topic
  • New Topic