Jump to content

Java Programming

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Pwincy Priya

Pwincy Priya

    Newbie

  • Members
  • PipPip
  • 22 posts
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.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
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);

  }

}
or
String pattern;

for(int i=0 ; i<7 ; i++){

  if(i%2==0){

    System.out.println("* * * * * * * * ");

  } else {

    System.out.println(" * * * * * * * *");

  }

}


#3
isuru

isuru

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 233 posts
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
Pwincy Priya

Pwincy Priya

    Newbie

  • Members
  • PipPip
  • 22 posts
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.

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
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
Pwincy Priya

Pwincy Priya

    Newbie

  • Members
  • PipPip
  • 22 posts
Oh okay..thanks a lot for your kind help.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users