mercredi 29 avril 2020

Printing a reflected hollow right triangle pattern (*) in Kotlin

I am currently taking a programming principles class with a Kotlin language focus at my school due to my growing interest in programming.

I am to create a program that takes user input for an integer. After I collect this information, I need to print two triangles in a asterisk (*) pattern (I had no problem completing these steps).

In addition, there is potential for extra credit which I am not working on more for experience than the extra points as I am looking good grade wise. I need to take the first triangle and reflect it while maintaining the properties. So far, I am unable to figure out how I would keep the triangle hollow and get the third side on to it.

My code:

fun main() {

    // Prompt user for the triangle's size
    print( "How big do you want your triangles? " )

    // Get the triangle's size from the user and store in a variable
    var triangleSize = readLine()!!.toInt()

    // Decrementor for internal spaces
    var decrementor = triangleSize

    // Incrementor for internal spaces
    var incrementor = 2

    // Case given a 0 is entered
    if ( triangleSize == 0 ) {

        // Case given 1 is entered
    } else if ( triangleSize == 1 ) {

        println( "*" )

        println( "1" )

        for ( i in 1..( triangleSize - 1 )) {

            print( " " )

        }

        print( "*" )

        // Case given number entered that is not 0 or 1
    } else {

        // Print the top point of the triangle
        println( "*" )

        // Print the sides of the triangle
        for ( i in 0..( triangleSize - 3 )) {

            print( "*" )

            for ( i in 1..( triangleSize - decrementor )) {

                print( " " )

            }

            println( "*" )

            decrementor--

        }

        // Print the bottom of the triangle
        for ( i in 1..triangleSize ) {

            print( "*" )

        }

        println()

        // Print the numbers triangle
        for ( i in 1..triangleSize ) {

            for ( j in 1..i ) {

                print( "$j" )

            }

            println()

        }

        // Print the top of the inverse triangle
        for ( i in 1..( triangleSize - 1 )) {

            print( " " )

        }

        println( "*" )

        for ( i in 1..( triangleSize - 2 )) {

            for ( i in 1..( triangleSize - incrementor )) {

                print( " " )

            }

            println( "*" )

            incrementor++

        }

        // Print the bottom of the inverse triangle
        for ( i in 1..triangleSize ) {

            print( "*" )

        }

    }

}




Aucun commentaire:

Enregistrer un commentaire