2013-10-19 73 views
1

想知道我爲我的visual basic計算作業做了什麼是正確的。我們的活動任務:使用case語句任務檢查的Visual Basic選擇

「活動:使用等級選擇Case語句

簡介:

您將使用一個case語句,將允許用戶輸入一個等級值作爲一個整數值,並返回級爲一個字母,從A到E

選擇邏輯

  • 如果輸入的號碼是91-100的輸出之間將A

  • 如果輸入的號碼是81和90之間的輸出會是B

  • 如果輸入的號碼是71和80之間的輸出將是C

  • 如果輸入的數是61和輸出70之間將是D

  • 如果輸入的號碼是51和60之間的輸出將通過電子郵件

    任何下50失敗
    任何高於100不正確的值,他們將不得不再次運行該程序。
    創建必要的變量
    創建必要輸出告訴用戶節目的目的 創建代碼在用戶的名字讀
    創建在等級讀取作爲整數值的代碼
    創建代碼根據上述標準生成相關等級。
    創建必要的輸出代碼輸出用戶的名字和他們的字母」的字母等級

我的代碼:

Module Module1 

Sub Main() 
    Dim anum As Integer 
    Dim name As String 
    Console.WriteLine("This programme converts marks into grades") 
    Console.WriteLine("Please enter name...") 
    name = Console.ReadLine 
    Console.WriteLine("Please enter number of marks...") 
    anum = Console.ReadLine() 
    Select Case anum 
     Case 91 To 100 
      Console.WriteLine(name & " receives an A.") 
     Case 81 To 90 
      Console.WriteLine(name & " receives a B.") 
     Case 71 To 80 
      Console.WriteLine(name & " receives a C.") 
     Case 61 To 70 
      Console.WriteLine(name & " receives a D.") 
     Case 51 To 60 
      Console.WriteLine(name & " receives an E.") 
     Case Is <= 50 
      Console.WriteLine(name & ", unfortunately failed.") 
     Case Is > 100 
      Console.WriteLine(name & ", this is an incorrect value. Please try again.") 
    End Select 
End Sub 

End Module 

將感謝,如果有人可以只確認它是正確或者告訴我,如果我做錯了或需要補充的東西!

感謝。

回答

0

我會做不同的比你原來的代碼的唯一的事情是: 使用「案例否則」取代您的最終案例聲明中的「Case Is> 100」。 這樣,如果有人輸入了數字以外的其他東西,它會處理一個錯誤。 良好的工作和祝你好運!

1

原始代碼似乎沒事就在我已經使用了正確的數據類型改進它,加上基本的異常鑄件,並試圖把事情簡單化:

Module Module1 

' Store ranges and chars 
ReadOnly TupleList As New List(Of Tuple(Of Short, Short, Char)) From { _ 
     Tuple.Create(51S, 60S, "E"c), _ 
     Tuple.Create(61S, 70S, "D"c), _ 
     Tuple.Create(71S, 80S, "C"c), _ 
     Tuple.Create(81S, 90S, "B"c), _ 
     Tuple.Create(91S, 100S, "A"c) _ 
} 

' Set custom strings formatting 
ReadOnly str_OK As String = "{0}, receives an {1}." 
ReadOnly str_FAIL As String = "{0}, unfortunately failed." 
ReadOnly str_INCORRECT As String = "{0}, this is an incorrect value. Please try again." 

Sub Main() 

    ' Initialize user variables with a default value (0) 
    Dim anum As Short = 0 
    Dim name As String = 0 

    Console.WriteLine("This programme converts marks into grades") 
    Console.WriteLine("Please enter name...") 
    name = CStr(Console.ReadLine) 

    Try 
     Console.WriteLine("Please enter number of marks...") 
     anum = CShort(Console.ReadLine()) 

    Catch ex As FormatException 
     Console.WriteLine("Please enter a valid number...") 
     Environment.Exit(1) ' Exit from application returning an error exitcode 

    Catch ex As Exception 
     Console.WriteLine(String.Format("{0}: {1}", _ 
             ex.Message, _ 
             ex.StackTrace)) 
     Environment.Exit(1) ' Exit from application returning an error exitcode 

    End Try 

    Select Case anum 

     Case Is <= 50 
      Console.WriteLine(String.Format(str_FAIL, name)) 

     Case Is >= 101 
      Console.WriteLine(String.Format(str_INCORRECT, name)) 

     Case Else ' User value is inside an accepted range 
      For Each Item As Tuple(Of Short, Short, Char) In TupleList 
       If (anum >= Item.Item1 AndAlso anum <= Item.Item2) Then 
        Console.WriteLine(String.Format(str_OK, name, Item.Item3)) 
        Environment.Exit(0) ' Exit from application 
        ' Exit For 
       End If 
      Next Item 

    End Select 

    Environment.Exit(1) ' Exit from application returning an error exitcode 
    ' (When Is <= 50 or Is >= 101) 

End Sub 

End Module 
+0

哦,哇,謝謝你這個隊友,但我絕對沒有到達這個階段vb與那麼多的複雜化。 還是謝謝! – Kcode1