Make a checkerboard in Java!

Anonymous user -  
 sauvage -
Hello, I have a problem: I need to draw a checkerboard in Java on Processing using for loops and if/else conditions, but I can't do it; it gives me black and white lines... I think the problem comes from the nesting of my for loops... but I can't fix it...

Configuration: Windows / Edge 15.15063

3 answers

  1. NHenry Posted messages 15235 Registration date   Status Moderator Last intervention   387
     
    Please post the code directly, as an image is unusable.
    (Note, I’m waiting for this before proposing a solution)

    --
    I mainly work in VB6 and VB.NET, with a bit of C#, but moderation often leads me to other languages.
    In VB.NET, remember to enable "Option Explicit" and "Option Strict"
    0
    1. Anonymous user
       
      Here is the code translated into English while preserving structure:

      size(500,500); for(int i=0; i<10; i++){ if(i%2!=0){ noStroke(); fill(255,255,255); } else{ noStroke(); fill(0,0,0); } for(int j=0; j<10; j++){ if(j%2!=0){ noStroke(); fill(255); } else{ noStroke(); fill(0); } rect(50*j,50*i,50,50);}}
      0
  2. KX Posted messages 19031 Status Moderator 3 020
     
    Hello,

    It would be nice to use the automatic formatting (Ctrl+T) to better see the nesting of the blocks with each other. Looking at your code like this, it seems the loops follow one another, whereas in reality they are nested.

    Otherwise, your first if is useless since the second for loop will redraw over it anyway.

    In any case, you cannot determine whether a square is white or black using only its row or only its column; you need both pieces of information at the same time to know which color to draw.

    Note: since you always have noStroke, there is no point in applying it every time; one time at the beginning is enough.

    --

    Trust does not exclude control.
    0
    1. Anonymous user
       
      Thank you very much! I’ll try to revise it.
      0
  3. sauvage
     
    Is there a way to make a checkerboard, but it’s a question of if you need 3 if statements, so 2 nested ones, I’ll search for the code.
    -1