Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java : i have to change the square to triangle

    hello i have to change the code of a program that draw a square with stars to a triangle square , can someone help me out please ?

    here is the code into 2 files :

    first files ---> Hollow.java

    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



    second file -----> HollowTest.java

    public class HollowTest
    {
    public static void main( String args[] )
    {
    Hollow application = new Hollow();
    application.drawHollowBox();
    } // end main
    } // end class HollowTest



    thank you very much

  • #2
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    What are you having problems with? We aren't going to do your work for you (as per our rules) however if you have a specific problem, we can help you with that.
    "To iterate is human, to recurse divine." -L. Peter Deutsch


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •