Para los siguientes bucles que muestran un valor incorrecto

The Code has two options. Option one starts with 1 dollar and doubles everyday for 10 days. If this is correct the value should be $1024 but instead it shows double that ($2046)

The second option starts with $100 and adds $100 per day for 10 days. The number should come out to an even $1000 but instead shows $6500

Public Class Form1
    Private Sub compareButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles compareButton.Click

        Dim initValue1 As Integer = 1
        Dim value1 As Integer
        Dim initValue2 As Integer = 100
        Dim value2 As Integer

        Integer.TryParse(Option1TextBox.Text, value1)
        Integer.TryParse(Option2TextBox.Text, value2)



        For day As Integer = 1 To 10
            initValue1 = initValue1 * 2
            value1 += initValue1
        Next day

        Option1TextBox.Text = value1.ToString("C2")

        For day As Integer = 1 To 10
            initValue2 = initValue2 + 100
            value2 += initValue2
        Next day

        Option2TextBox.Text = value2.ToString("C2")

    End Sub
End Class

I feel like I am making a very small mistake. Any help?

preguntado el 02 de febrero de 12 a las 10:02

I suggest you set up break points in inside your For loops so that you can actually observe what is happening. This isn't really a code issue but a logic issue. Let me know if you need to know how to use break points. Good luck! -

1 Respuestas

Prueba esto:
No Need of value1 += initValue1 y la value2 += initValue2 remove them and try


Dim initValue1 As Integer = 1
        Dim value1 As Integer
        Dim initValue2 As Integer = 100
        Dim value2 As Integer
        Integer.TryParse(Option1TextBox.Text, value1)
        Integer.TryParse(Option2TextBox.Text, value2)
        For day As Integer = 1 To 9
            initValue1 = initValue1 * 2
        Next
        Option1TextBox.Text = initValue1.ToString("C2")
        For day As Integer = 1 To 9
            initValue2 = initValue2 + 100
        Next
        Option2TextBox.Text = initValue2.ToString("C2")

enter image description here

Respondido 02 Feb 12, 15:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.