How do i repeat this " * " symbol by using loop in order to display a checkerboard pattern as follow:
* * * * * * * *
* * * * * * * * (There should be a space at the beginning of the pattern for this line)
* * * * * * * *
* * * * * * * * (Same goes to this line)
* * * * * * * *
* * * * * * * * (Same goes to this line)
* * * * * * * *
* * * * * * * * (Same goes to this line)
Can anyone help me out how to create such a checkerboard pattern using loop. Thanks.
5 replies to this topic
#1
Posted 06 November 2010 - 01:42 AM
|
|
|
#2
Posted 06 November 2010 - 02:04 AM
String pattern;
for(int i=0 ; i<7 ; i++){
if(i%2==0){
pattern = "* ";
} else {
pattern = " *";
}
for(int j=0 ; j<8 ; j++){
System.out.println(pattern);
}
}
orString pattern;
for(int i=0 ; i<7 ; i++){
if(i%2==0){
System.out.println("* * * * * * * * ");
} else {
System.out.println(" * * * * * * * *");
}
}
#3
Posted 06 November 2010 - 02:13 AM
package isuru;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
public class Main{
public static void main(String args[]){
JFrame checkerBoard = new JFrame();
checkerBoard.setSize(400, 400);
checkerBoard.setTitle("Checker Board");
checkerBoard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int rows = 5;
int cols = 5;
Container pane = checkerBoard.getContentPane();
pane.setLayout(new GridLayout(rows, cols));
Color checker;
for(int x = 1; x <=(rows*cols); x++){
int altr = 0;
altr = (x-1) % cols;
altr += (x-1)/cols;
if(altr % 2 == 0){
checker = Color.black;
} else {
checker = Color.red;
}
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(400/rows, 400/cols));
panel.setBackground(checker);
pane.add(panel);
}
checkerBoard.setVisible(true);
}
}
Here, I done it. And oxano is right!
Attached Files
Lost!
#4
Posted 06 November 2010 - 03:53 AM
To oxano,
Hi, your second code met my requirement but i have a problem. I have to use only 3 output statements as follow:
System.out.print( "* " );
System.out.print( " " );
System.out.println();
i have to create the checkerboard pattern using the 3 output statements above only. So can u please help me with that. Thanks.
Hi, your second code met my requirement but i have a problem. I have to use only 3 output statements as follow:
System.out.print( "* " );
System.out.print( " " );
System.out.println();
i have to create the checkerboard pattern using the 3 output statements above only. So can u please help me with that. Thanks.
#5
Posted 06 November 2010 - 03:56 AM
for(int i=0 ; i<7 ; i++){
if(i%2!=0){
System.out.print( " " );
}
for(int j=0 ; j<8 ; j++){
System.out.print( "* " );
}
System.out.println();
}
It will print lines that end with a * with an extra space behind, but since there's no print that ends with a * that's the only choice you have.On a console that space isn't really visible anyway.
#6
Posted 06 November 2010 - 04:17 AM
Oh okay..thanks a lot for your kind help.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top










