• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

assignment 4a: how to extract the print statement?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need some help for the Say assigment. I use 2 arrays, one for the numbers less than 20, another for the tens(20, 30..). here is my code:
if number less than 20
print units[]
else if number is tens
print tens[]
else
print tens[] + "-" + unit[]
I was nitpicked for printing tens twice here. but how do I extract the print statement out of the if-else clause?
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your first if-clause is fine. But you only need one more, for the case that the number is 20 or greater. Then, within that second clause, create another clause testing whether the number is an even multiple of ten or not. If it is, add the hyphen-ones. If it is not, no more printing needed.
Thus, no duplicate print statements.
------------------
  • Ryan Burgdorfer
  • Java Acolyte
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic