使用whileloop的VB.NET总和

时间:2014-10-01 19:15:25

标签: vb.net while-loop sum

我已经设法编写了对值进行求和的代码,但前提是我知道用户希望从头开始求和的数量(使用数组)。

如果用户首先输入他/她想要求和的值的数量,这是我的代码可以正常工作:

Public Class WholeNumbersForAdd
    Private numberOfVal As Integer
    Private sum As Integer
Private Sub NumbOfVal()
        Console.Write("Numbers of values to sum? ")
        numberOfVal = CInt(Console.ReadLine())
        Console.WriteLine()
    End Sub

    Private Sub enterVal()
        Dim nums(numberOfVal) As Integer


        For i As Integer = 1 To numberOfVal Step 1
            Console.Write("Please give the value no " & i & " (whole number): ")
            nums(i) = CInt(Console.ReadLine)
        Next 
        For Each value As Integer In nums
            sum = sum + value
        Next

    End Sub

现在,我正在尝试使用while循环对不确定数量的值进行求和,但我似乎无法掌握While循环,我必须在本练习中使用。我希望程序继续使用新值(每行一个值),直到用户按下Q按钮。当用户按Q时,我希望程序汇总用户输入的所有值。

这是我到目前为止所得到的,但我知道我远离解决方案。

    Private Sub enterVal()

    Dim nums As Double
    Dim inputValue As Double
    Dim sumValues As Double

    While (inputValue <> Q)
        Console.Write("Write an amount to sum or press Q to finish: ")
        nums(inputValues) = CDbl(Console.ReadLine)

    End While

    For Each value As Integer In inputValue
        sumValues = sumValues + value
    Next

End Sub

顺便说一下。我不被允许使用&#34; Do Loop While&#34;。

2 个答案:

答案 0 :(得分:1)

您要通过Console.ReadLine输入值,以便While (inputValue <> Q)无法正常使用

While True
    Console.Write("Write an amount to sum or press Q to finish: ")
    Dim inputString as String = Console.ReadLine
    If inputString.ToUpper = "Q" Then Exit While
    nums(inputValues) = CDbl(inputString)
End While

你在while中不需要一个值,无限循环,因此While True 将输入转换为字符串,因为您期望Q,它不能是您示例中的数字。 检查您是否输入了q或Q(如果您不需要检查小写q,请删除.ToUpper)并退出循环(如果是)。

总体设计不是很好,但如果你的任务属于你的一部分我留下了。

注意:以上代码未经过测试

答案 1 :(得分:0)

Module Module1
    Sub Main()

        Dim m, n, c, d As Int16

        Dim first(,) As Int16 = New Int16(5, 5) {} 
        Dim second(,) As Int16 = New Int16(5, 5) {} 
        Dim sum(,) As Int16 = New Int16(5, 5) {}

        Console.WriteLine("Enter the number of rows and columns of matrix")

        m = Convert.ToInt16(Console.ReadLine())

        n = Convert.ToInt16(Console.ReadLine())

        Console.WriteLine("\nEnter the elements of first matrix\n")

        c = 0
        While c < m
            d = 0
            While d < n

                Console.WriteLine("Enter Element [" + c.ToString() + " , " + d.ToString() + "]") 
                first(c, d) = Convert.ToInt16(Console.ReadLine())
                d = d + 1

            End While
            c = c + 1 
        End While 
        Console.WriteLine("Enter the elements of second matrix") 
        c = 0
        While c < m
            d = 0
            While d < n 
                Console.WriteLine("Enter Element [" + c.ToString() + " , " + d.ToString() + "]") 
                second(c, d) = Convert.ToInt16(Console.ReadLine()) 
                d = d + 1 
            End While
            c = c + 1
        End While

        c = 0
        While c < m
            d = 0
            While d < n
                 sum(c, d) = first(c, d) + second(c, d)
                 d = d + 1
             End While
            c = c + 1
        End While
         Console.WriteLine("Sum of entered matrices:-")
         c = 0
        While c < m
            d = 0
            While d < n
                 Console.Write("  " + sum(c, d).ToString())
                 d = d + 1
             End While
            Console.WriteLine()
            c = c + 1
        End While
         Console.ReadKey()
     End Sub
End module