Search code examples
vb.netconsole-applicationsubtractionbasic

Integer subtraction and looping based on integer values


I'm having two problems here. First off all I want to x to change values to x - y and if the new X is higher then 0 i want the process to repeat itself. I've worked out the code below but i'm not certiant on two things.

  1. Am I even allowed to make the equation x = x - y or does this mess up everything? I mean in mathematical terms it would not be possible but if we take X as Hp and Y as damage I want the damage to add up. I don't want it to create an "damage HP" integer for every subtraction as I even don't know how many "Z = x - y" style equations I would have to create if I set Y to be random. My guess is that I could create a Z integral that would copy X a moment before the subtraction would go off and then have the subtraction be X = Z - Y but I'm not sure how I would go about coding this.

  2. I want it to go ahead and loop itself if X is higher then 0 and I'm not sure if I coded that correctly.

Here is my code:

Module Module1
        Dim A As Integer
        Dim B As Integer
        Dim x As Integer
        Dim y As Integer
        Sub Main()

    End Sub

    Sub Maths()
        A = 5
        B = 4
        x = 3
        y = 1
        Subtraction()
        Console.WriteLine("You deal {0} damage to your enemy reducing it to {1} hp.", y, x)
            Do Until x <= 0

            Loop
    End Sub
    Private Sub Subtraction()
        If A > B Then x = x -y
            Return
    End Sub

End Module

Solution

  • I liked this question. Here's my thoughts:

    1. Yes, x = x - y is perfectly valid code. It's no different than if I had a string variable named myRunOnSentence and I wanted to concatenate the string that was already in the variable and another string and then store the results back in the string variable. Like this: myRunOnSentence = myRunOnSentence + "another string" Same concept, just change the datatype to an Integer. x = x + y. That programatically says: "take the value in x and the value in y, add them together, and store the result of that expression in x."

    2. You did indeed make a mistake with the loop. You don't have any code inside the body of the loop itself.

    3. You have nothing happening in the Main() sub of your module so this module when run will do nothing. You should just take the code from the Maths() method and put it in the Main() sub.

    4. In your Subtraction() method, A > B will always evaluate to True because A and B are initialized with values and then never changed.

    Your code should look something like this:

    Module Module1
        Dim A As Integer = 5
        Dim B As Integer = 4
        Dim x As Integer = 3
        Dim y As Integer = 1
    
        Sub Main()
            Do Until x <= 0
                Subtraction()
                Console.WriteLine("You deal {0} damage to your enemy reducing it to {1} hp.", y, x)
            Loop
        End Sub
    
        Private Sub Subtraction()
            If A > B Then x = x - y 'Return statement wasn't needed.
        End Sub
    End Module
    

    If this answered your question, please don't forget to mark it as the answer.