2014-10-01 89 views
0

我已經設法編寫了總結值的代碼,但前提是我知道用戶想要從頭開始總結多少個值(使用數組)。使用whileloop的VB.NET總和

這裏是我的代碼工作正常,如果用戶首先進入他/她想要總結值的數量:

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時,我希望程序將用戶輸入的所有值相加。

這是我到目前爲止,但我知道我離開解決方案faaaaaar。

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 

Btw。我不允許使用「Do Loop While」。

+1

你是什麼意思獲准使用「Do循環而」 _ _not,這是一個家庭作業的問題? – 2014-10-01 19:20:15

+0

使用'List'而不是數組,並且'Add'添加每個新輸入。 – crashmstr 2014-10-01 19:25:29

回答

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 True 取輸入一個字符串,因爲你期待Q,它不能像你的例子那樣是一個數字。 檢查您是否輸入了q或Q(如果不需要檢查小寫字母q,則刪除.ToUpper),如果是,則退出循環。

一般的設計不是一個好的設計,但是如果這是你作業的一部分,我就把它留下。

注:上面的代碼沒有進行測試

+0

謝謝@George,我使用了大部分代碼,不得不做一些小的調整。我非常感謝! – Televinken 2014-10-01 20:34:16

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