【问题标题】:Visual basic turning array integers (x) into x amount of a single characterVisual basic将数组整数(x)转换为单个字符的x量
【发布时间】:2012-03-31 14:19:41
【问题描述】:

我的任务是创建一个 Visual Basic 控制台脚本,它要求用户连续 5 次将一个数字输入一个数组(销售数字以千为单位),然后将这些结果显示为一种计数图表。

例如数据:sales(10,7,12,5,15) 输出将是

2008:++++++++++

2009:+++++++

2010:++++++++++++

2011:+++++

2012:+++++++++++++++

到目前为止我拥有的代码:

Module Module1

Sub Main()

    Dim sales(4) As Integer
    Dim index As Integer
    Dim year As Integer


    For index = 0 To 4
        Console.Write("Enter your sales numbers (in thousands): ")
        sales(index) = Console.ReadLine()
    Next

    year = 2007

    For index = 0 To 4
        year = (year + 1)

---不确定这里的代码---

        Console.WriteLine(year & ": " & ????????)
    Next

    Console.ReadLine()
End Sub

End Module

我只是不确定如何将数组中的整数值更改为一定数量的单个字符。

【问题讨论】:

    标签: vb.net arrays visual-studio integer


    【解决方案1】:
    For Each i As Integer In Sales
        Console.WriteLine(New String("+"c, i))
    Next i
    

    【讨论】:

    • 为什么不只是 New String("+"c, i) ??
    • 因为我总是忘记构造函数重载;)
    【解决方案2】:

    添加一个for来显示多次“-”不就足够了吗?

    这样:

    For index = 0 To 4
        year = (year + 1)
        Console.Write(year & ": ")
    
        ' Display as much "-" as there are sales
        For s = 1 to sales(index)
            Console.Write("-")
        Next s
    
        Console.WriteLine("") 'Next line
    Next index
    

    【讨论】:

    • 这仍然为我提供了 1 个额外,比如我输入了 sales(3,2,3,4,3) 输出显示 4,3,4,5,4
    • 是的,我的错误,我从 s = 0 开始 for..next 我应该写“For s = 1 to sales(index)”
    • 如果这个问题解决了,你应该点击帮助你解决它的答案旁边的勾选接受答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 2016-11-06
    • 2013-04-05
    • 2020-01-03
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    相关资源
    最近更新 更多