Java Programming nested loops -


so have of code written, there's 1 part of assignment don't understand. write program accept number (n) user represent size of board (nxn). if user not enter number greater 1, prompt user on , on until he/she gives valid input.

once valid input obtained, print board every other column filled 1s along last row filled 1s. zeros everywhere else. board have equal number of rows , columns based on users input.

i have pattern 0's , 1's don't understand how can last row have 1's. here code posted below

import java.util.scanner; public class question1 { public static void main(string[]args) { scanner input = new scanner(system.in); int n; system.out.println("please input value board greater 1."); n= input.nextint(); while(n<1) {     system.out.println("error, please enter value greater 1");     n=input.nextint(); } for(int i=0; i<n; i++) {     for(int j=0; j<n; j++)     {          if(j%2==0)         {             system.out.print(0);         }         else         {             system.out.print(1);         }         if(i==n)         {             system.out.print(1);         }       }     system.out.println(' '); } } } 

change loops to:

for (int = 0; < n; i++) {   (int j = 0; j < n; j++) {     if (i == n - 1) {       system.out.print(1);     } else {       system.out.print(j % 2);     }   }   system.out.println(); } 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -