Java: Hollow square

Hallow square,
Ini code untuk class Hollow


Class Hollow:
import java.util.Scanner;

public class Hollow
{
// draw a hollow box surrounded by stars
public void drawHollowBox()
{
Scanner input = new Scanner( System.in );

int stars; // number of stars on a side
int column; // the current column of the square being printed
int row = 1; // the current row of the square being printed

// prompt and read the length of the side from the user
System.out.print( "Enter length of side:" );
stars = input.nextInt();

if ( stars < 1 )
{
stars = 1;
System.out.println( "Invalid Input\nUsing default value 1" );
} // end if
else if ( stars > 20 )
{
stars = 20;
System.out.println( "Invalid Input\nUsing default value 20" );
} // end else if

// repeat for as many rows as the user entered
while ( row <= stars )
{
column = 1;

// and for as many columns as rows 
while ( column <= stars )
{
if ( row == 1 )
System.out.print( "*" );
else if ( row == stars )
System.out.print( "*" );
else if ( column == 1 )
System.out.print( "*" );
else if ( column == stars )
System.out.print( "*" );
else
System.out.print( " " );

column++;
} // end inner while loop

System.out.println();
row++;
} // end outer while loop
} // end method main
} // end class Hollow
Dan ini code untuk class mainnya


Class HollowTest:
public class HollowTest
{ 
public static void main( String args[] )
{
Hollow application = new Hollow();
application.drawHollowBox();
} // end main
} // end class HollowTest
Code diatas saya dapat dari forum java dan untuk lebih jelasnya silahkan di click disini.
dan ini hasilnya setelah saya RUN di netbeans.
Output:

Komentar

Postingan populer dari blog ini

Batu Kertas Gunting

Main Bumerang

Menambah Space di Drive C