• 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

Improve this?

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyway i can improve this code? Make it more robust and shorter.

 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this a homework problem or real code from somewhere?
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Scott Selikoff:
Is this a homework problem or real code from somewhere?



Its my complete assignment, its working, im just looking for tips on how i could make it more robust etc.
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Comments>

- call int minustwo as minusTwo, just looks better and more in standard

- that if ( if (number <=2 && number >=1) { line3 = line4;}) executes on 1 & 2
could be written as if (number ==2 || number ==1) { line3 = line4;}, IMHO it looks clearer

- this
System.out.println("\n"+ number +line1);
System.out.println(number + line2);
System.out.println(line3);
System.out.println();

could be
System.out.println("\n"+ number +line1+"\n"+number + line2+"\n"+line3+"\n");
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well...

You have a for loop that isn't doing anything. You could either do:


OR
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gerardo Tasistro:
- this
System.out.println("\n"+ number +line1);
System.out.println(number + line2);
System.out.println(line3);
System.out.println();

could be
System.out.println("\n"+ number +line1+"\n"+number + line2+"\n"+line3+"\n");



It could be, but it shouldn't be. Five calls
to println() is the preferred way, but if you
really want to do it in a single call I guess
you could do it like this:

String sep = System.getProperty("line.separator");
System.out.println(sep + number + line1 + sep + number + line2 + sep + line3 + sep);

or:

System.out.print(sep + number + line1 + sep + number + line2 + sep + line3 + sep + sep); // make final newline explicit

There's a good FAQ answer out there somewhere that touches
on this, but I couldn't find it when I searched briefly.
 
I'm all tasted up for a BLT! This tiny ad wants a monte cristo!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic