必须制作一个制作金字塔的程序

时间:2016-01-27 12:54:20

标签: vb.net

我已经掌握了基础知识但是如何输入每行中的空格数?

这是我到目前为止所做的事情:

Sub Main()
    Dim var As Char
    Dim numberofsymbols As Integer

    Console.WriteLine("what variable do you want to use to form a pyramid?")
    var = Console.ReadLine

    Console.WriteLine("what number of symbols do you want to start with? (odd number)")
    numberofsymbols = Console.ReadLine()

    'taking numberofsymbols as 5

    Console.Write("   ")
    Console.Write(var)


    Console.WriteLine()
    Console.Write("  ")


    For x = 1 To numberofsymbols - 2
        Console.Write(var)
    Next

    Console.WriteLine()
    Console.Write(" ")


    For x = 1 To numberofsymbols
        Console.Write(var)
    Next

    Console.ReadKey()

End Sub

我得到的输出是

    A

   AAA

  AAAAA

我没有在我的程序中检查它是否是一个奇数/一个字符,如果用户选择一些随机的奇数,我应该如何制作这个程序呢?

我知道不得不使用重复直到循环,但那么如何输入循环中的空格数呢?

3 个答案:

答案 0 :(得分:0)

If NOT NUMBER Mod 2 = 0 Then NUMBER -= 1

'Do your pyramid here

如果它的偶数(除以2而没有余数),则Mod 2将返回0,否则将返回余数。

Number是用户输入。

答案 1 :(得分:0)

下面:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

如果你想在每一行后添加一个空行,你可以添加以下行:

Sub Main()
    Dim var As Char
    Dim numberofsymbols As Integer

    Console.WriteLine("what variable do you want to use to form a pyramid?")
    var = Console.ReadLine
    Console.WriteLine("what number of symbols do you want to start with? (odd number)")
    numberofsymbols = Console.ReadLine()
    If numberofsymbols Mod 2 = 0 Then numberofsymbols += 1 'to check if the number is even and add '1' to make it odd, you can use 'numberofsymbols -=1' to subtract insead of adding

    Dim spaces As String
    Dim vars As String
    For i = 1 To numberofsymbols Step 2
        spaces = New String(" ", (numberofsymbols - i) / 2)
        vars = New String(var, i)
        Console.WriteLine(spaces + vars)
    Next

    Console.ReadKey()
End Sub

希望这会有所帮助:)

答案 2 :(得分:0)

Dim space, maxsymbol, noofsymbol As Integer
        Dim var As Char

        Console.WriteLine("enter char to use")
        var = Console.ReadLine
        Console.WriteLine("enter max rows(odd number)")
        maxsymbol = Console.ReadLine



        space = ((maxsymbol - 1) / 2)
        noofsymbol = 1

        Do
            For i = 1 To space
                Console.Write(" ")
            Next

            For x = 1 To noofsymbol
                Console.Write(var)
            Next

            Console.WriteLine()

            space -= 1
            noofsymbol += 2



        Loop Until noofsymbol > maxsymbol



        Console.ReadKey()