我需要一些關于矩形計算器代碼的幫助......所以我制作了一個矩形面積計算器,它具有長度、寬度、面積、矩形數量和最小矩形......我還創建了一個處理無效的捕獲塊 -強制提取其中的 2 個......當結果的值大于 1 百萬時也是一個期望類......這是我的代碼
Public Class Form1
Dim area As Decimal
Dim numberofRectangles As Integer
Dim smallestRectangle As Decimal
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Try
Dim length As Decimal = CDec(txtLength.Text)
Dim width As Decimal = CDec(txtWidth.Text)
Dim Area As Decimal = width * length
Dim numberofRectangles As Integer = numberofRectangles 1
Dim smallestRectangle As Decimal = Math.Min(smallestRectangle, Area)
txtArea.Text = Area.ToString("n2")
txtNumberOfRectangles.Text = numberofRectangles.ToString
txtSmallestRectangle.Text = smallestRectangle.ToString("n2")
txtLength.Select()
Catch ex As InvalidCastException
MessageBox.Show("Please check entries for valid numeric data",
ex.GetType.ToString)
Catch ex As OverflowException
MessageBox.Show("Please check to make sure entries aren't too large.",
ex.GetType.ToString)
Catch ex As Exception
MessageBox.Show(ex.Message & vbNewLine & vbNewLine & ex.StackTrace,
ex.GetType.ToString)
If area < 1000000000 Then
Throw New FormatException("The rectangle Is too large!")
Return
End If
Finally
End Try
End Sub
對于一個我有一個錯誤,我的最小矩形是 999,999,999,00 并且當結果的值大于 100 萬時,我遇到了麻煩,正如您從代碼中看到的那樣。尋找有關我的代碼的一些建議
編輯:修復頂部現在在最小的矩形框中獲得 0.00
我應該在某個地方使用 me.compute 嗎
uj5u.com熱心網友回復:
你拼錯了smallestRecntangle——這就是你需要重新宣告它的原因——它與類變數不同smallestRectangle。出于這個原因,smallestRectangle除了 999999999 之外,您永遠不會設定為任何其他值。此外 - 您可以使用呼叫的值Decimal.MaxValue來執行與 999999999 相同的操作,但使用實際最大可能的十進制數。但是,我建議您Decimal?最初使用并分配給 null。在代碼中,檢查“如果smallestRectangle 為null,則設定smallestRectangle,否則將其設定為smallestRectangle 和area 中的最小值”。
uj5u.com熱心網友回復:
例外處理用于處理您無法控制的意外錯誤。我們可以測驗用戶輸入,因此我們不需要例外處理。
ex.GetType.ToString
這沒有意義。如果要顯示Exception訊息,可以使用和符號&和ex.Message。ex.ToString通常提供的資訊比您希望用戶看到的要多。
TryParse將檢查第一個引數中提供的字串是否可以轉換。它回傳True或False。如果True它將轉換后的字串分配給第二個引數。
numberofRectangles = 1
是一種快捷的寫作方式
numberofRectangle = numberofRectangles 1
唯一可能引發例外的代碼行是面積計算,因此我們將 Try 塊限制在該行。
使用你的主要問題是Dim陳述句numberofRectangles和smallestRectangle里面的Sub。這些變數在 處松散值End Sub。它們只存在于Sub. 盡管Dim與Private宣告Class級別變數的含義相同,但Private首選。Dim用于方法中的區域變數。
為了避免最小矩形始終為零的問題,我檢查了計數并設定smallestRectangle為第一個矩形區域。在那之后Math.Min接管。
Private numberofRectangles As Integer
Private smallestRectangle As Decimal
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim length As Decimal
Dim width As Decimal
If Not Decimal.TryParse(txtLength.Text, length) Then
MessageBox.Show("Please check entries for valid numeric data")
Exit Sub
End If
If Not Decimal.TryParse(txtWidth.Text, width) Then
MessageBox.Show("Please check entries for valid numeric data")
Exit Sub
End If
Dim Area As Decimal
Try
Area = width * length
Catch ex As OverflowException
MessageBox.Show("Please check to make sure entries aren't too large." &
ex.Message)
Exit Sub
Catch ex As Exception
MessageBox.Show(ex.Message)
Exit Sub
End Try
numberofRectangles = 1
If numberofRectangles < 2 Then
smallestRectangle = Area
Else
smallestRectangle = Math.Min(smallestRectangle, Area)
End If
txtArea.Text = area.ToString("n2")
txtNumberOfRectangles.Text = numberofRectangles.ToString
txtSmallestRectangle.Text = smallestRectangle.ToString("n2")
txtLength.Select()
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/379273.html
標籤:网络 视觉工作室-2015
上一篇:在sklearn中使用基于組的驗證方法搜索模型的超引數
下一篇:呼叫水晶報表檔案
