2014-11-18 3 views
-1

Visual Basic 2010에서 시간당 임금을 연간 수 (예 : 10 * 40 * 52)로 계산하고 그 해마다 증가하는 비율을 10 년 이상으로 계산하는 프로그램을 작성하려고합니다. 내 주요 문제는 내가 수학을 9 번 반복하는 것으로 가정 된 다음 루프 (첫 번째는 고정됨)에 도달하면 코드가 첫 번째 반복의 값을 저장하고 다음 8 번의 반복에서이를 사용한다는 것을 알게된다. . 어떻게 이런 일이 일어나지 않도록 할 수 있습니다. 나는 그것을 단순하게 유지하려고 프로그래밍에 익숙하지 않다는 것을 명심하십시오. 감사!잘못 계산 한 다음 루프의 경우?

option Strict On 

Public Class frmPayCalculator 

Private Sub btnComputeFuturePay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComputeFuturePay.Click 
    ' The btnComputeFuturePay event accepts accepts hourly wage and pay raise 
    ' information, and then displays the yearly pay over the next ten years. 

    Dim decHourlyWage As Decimal 
    Dim decYearlyWageComputed As Decimal 
    Dim decExpectedRaise As Decimal 
    Dim decExpectedRaiseComputed As Decimal 
    Dim intYears As Integer 
    Dim decIncrementedYearlyWage As Decimal 

    If IsNumeric(txtHourlyWage.Text) Then 
     ' Convert input to decimal. 
     decHourlyWage = Convert.ToDecimal(txtHourlyWage.Text) 
     ' Data validation to check if input is a positive. 
     If decHourlyWage > 0 Then 
      decYearlyWageComputed = decHourlyWage * 40 * 52 
      lstDecadeWage.Items.Add("Year 1" & " - Wage: " & decYearlyWageComputed.ToString("C")) 
     Else 
      ' Error for negative number 
      lstDecadeWage.Items.Clear() 
      txtHourlyWage.Focus() 
      MsgBox("You have entered a negative number for Hourly' Data validation to check if input is a number. Rate, try again with a positive number", , "Input Error") 
     End If 
     ' Data validation to check if input is a number. 
     If IsNumeric(txtExpectedRaise.Text) Then 
      ' Convert Data to decimal 
      decExpectedRaise = Convert.ToDecimal(txtExpectedRaise.Text) 
      ' Divide the whole number by 100 to make it a percent. 
      decExpectedRaiseComputed = decExpectedRaise/100 
     Else 
      ' Error for non-number: Expected Raise percent. 
      lstDecadeWage.Items.Clear() 
      txtHourlyWage.Focus() 
      MsgBox("You have entered a non-number for the Expected Raise, try again with a number.", , "Input Error") 
     End If 
    Else 
     ' Error for non-number: Hourly Rate. 
     lstDecadeWage.Items.Clear() 
     txtHourlyWage.Focus() 
     MsgBox("You have entered a non-number for Hourly Rate, try again with a number.", , "Input Error") 
    End If 

    For intYears = 2 To 10 
     decIncrementedYearlyWage += (decYearlyWageComputed + decYearlyWageComputed * decExpectedRaiseComputed) 
     lstDecadeWage.Items.Add("Year " & intYears & " - Wage: " & decIncrementedYearlyWage.ToString("c")) 
    Next 

End Sub 

Private Sub mnuClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClear.Click 
    ' The mnuClear click event that clears the ListBox object, Wage and Raise Textboxes and, enables the Compute 
    ' Future Pay button. 

End Sub 

Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click 
    ' The mnuExit click event closes the window and exits the application. 
    Close() 

End Sub 


Private Sub frmPayCalculator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    'Preload event that clears the ListBox object, Wage and Raise Textboxes and, enables the Compute 
    ' Future Pay button. 

End Sub 
End Class 

답변

0

대신 :

: 수식의 값이,이 같은 뭔가를 찾고 될 수 변경되지 않기 때문에 같은 결과를 각각의 반복을 줄 것입니다

decIncrementedYearlyWage += (decYearlyWageComputed + decYearlyWageComputed * decExpectedRaiseComputed) 

예를 들어

decIncrementedYearlyWage += (decIncrementedYearlyWage * decExpectedRaiseComputed) 
+0

내가 내 말에 약간의 논리 오류로 실행중인 것을 알고 있었다. 처음에는 솔루션이 작동하는 이유를 이해하지 못했지만 완벽하게 이해할 수 있습니다. 반복이 실행될 때마다 반복의 값이 저장되고 계속해서 사용됩니다. 따라서 목록 상자의 값은 증가하지만 첫 번째 값의 값에 의해서만 증가합니다. – kl654

0

이러한 데이터 HourlyWage = 5, ExpectedRaise = 10

, 당신이하려고하는 경우 다음

Year 1 - Wage: $ 10,400.00 
Year 2 - Wage: $ 11,440.00 
Year 3 - Wage: $ 12,584.00 
Year 4 - Wage: $ 13,842.40 
Year 5 - Wage: $ 15,226.64 
Year 6 - Wage: $ 16,749.30 
Year 7 - Wage: $ 18,424.23 
Year 8 - Wage: $ 20,266.66 
Year 9 - Wage: $ 22,293.32 
Year 10 - Wage: $ 24,522.66 

그런 다음 당신이해야합니다

For intYears = 2 To 10 
    decYearlyWageComputed += decYearlyWageComputed * decExpectedRaiseComputed 
    lstDecadeWage.Items.Add("Year " & intYears & " - Wage: " & decYearlyWageComputed.ToString("c")) 
Next 
관련 문제