File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A better way with the if statement?

 
David Herron
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! first timer here.

I am working on a school project where I need to draw a wall of bricks with drawRect based on user input. This input asks for how many rows they would like to see. There can be up to 20 rows of bricks. Every odd row needs to be indented a half size. I am OK with the input part. I am currently working on drawing the rows. They way I am doing this works, but there must be an easier way. It is very repetitive.. Here is my code. (numRows is the value they enter). Thanks!

 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a number of ways of simplifying this. You could start by getting rid of the duplicated lines of code. If instead of "== 1", "== 2", etc, you wrote ">= 1", ">= 2" etc., what would the code look like?
 
Paul Clapham
Sheriff
Pie
Posts: 20171
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would refactor those if's like this:

I would likely do more refactoring if I knew what the code was supposed to be doing.
 
David Herron
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WHAT SPEED!

Thank you guys! You just saved me a TON of time. But how does it know to fill in the previous rows? It works, but I am a little confused on why.

Sorry if it should be hitting me in the face.

 
Paul Clapham
Sheriff
Pie
Posts: 20171
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take your pencil and paper and write down which lines of code are executed when (for example) numRows is 3.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic