In: Computer Science
How can I do the star pattern looks like: ( for example)
( you might not notice the difference but there is space at the beginning of row 2 & 4)
.* * * * *
. * * * * *
.* * * * *
. * * * * *
.* * * * *
instead of :
. * * * * *
. * * * * *
. * * * * *
. * * * * *
. * * * * *
This is the program that I'm trying to do :
import java.util.Scanner;
public class checkerBoard {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int number;
System.out.print("enter one integer value: ");
number = scan.nextInt();
for (int i = 0; i < number; i++)
{
for (int j = 0; j < number; j++)
{
System.out.print(" *");
}
System.out.println();
}
}
}
In case of any query do comment. Thanks
I have changed your code, in such cases you have to see the row number which you are trying to print have odd value at index then print extra space in outer loop for i.
Code:
import java.util.Scanner;
public class checkerBoard {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int number;
System.out.print("enter one integer value: ");
number = scan.nextInt();
for (int i = 0; i < number; i++)
{
if (i % 2 ==1)
//print a space for each odd row (used odd becuase index starts from 0 for first row )
System.out.print(" ");
for (int j = 0; j < number; j++)
{
System.out.print(" *");
}
System.out.println();
}
}
}
========Screen shot of the code=======
output: