So I have most of my code written, but there's one part of the assignment that I don't understand. Write a program that will accept a number (n) from the user to represent the size of a board (nxn). If the user does not enter a number greater than 1, prompt the user over and over until he/she gives valid input.
Once valid input is obtained, print a board with every other column filled with 1s along with the last row filled with 1s. Zeros everywhere else. Your board will have an equal number of rows and columns based on the users input.
I have it to do the pattern with the 0's and 1's but I don't understand how I can get the last row to have all 1's. Here is my 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 a value for the board greater than 1.");
n= input.nextInt();
while(n<1)
{
System.out.println("Error, please enter a value greater than 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(' ');
}
}
}
while(n<1)you should usewhile(n<=1)