JAVA HELP please public static void drawRow (Graphics g, int x, int y, int numbe
ID: 3922052 • Letter: J
Question
JAVA HELP please
public static void drawRow (Graphics g, int x, int y, int numberPairs, int sizeBox){
for(int i = 0; i < numberPairs; i++){
g.setColor(Color.BLACK);
g.fillRect(x + 2 * i * sizeBox, y, sizeBox, sizeBox);
g.setColor(Color.BLUE);
g.drawLine(x + 2 * i * sizeBox, y, x + 2 * i * sizeBox + sizeBox, y + sizeBox);
g.drawLine(x + 2 * i * sizeBox, y + sizeBox, x + 2 * i * sizeBox + sizeBox, y);
g.setColor(Color.WHITE);
g.fillRect(x + 2 * i * sizeBox + sizeBox, y, sizeBox, sizeBox);
}
}
Is there a simpler way to write this for loop, especially for the parts that are bolded. This is JAVA.
Explanation / Answer
One way you can do this above given code block to write efficiently and in a simpler way is by taking out the common factor in the statements within the loop and execute it once and save it to a variable. Then afterwards, instead of computing its value again and again, just put the variable name there...thats it. It will look like shown below:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.