Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

kindly fix these codes i am tryin to use function but it is not working thanks P

ID: 3760504 • Letter: K

Question

kindly fix these codes

i am tryin to use function but it is not working thanks

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim P1_val As Integer
        Dim P2_val As Integer
        Dim rand As New Random
        Lets_Play(P1_val, rand)
        Lets_Play(P2_val, rand)


        'P1_val = rand.Next(3) + 1
        'P2_val = rand.Next(3) + 1

        If (P1_val = 1) Then
            PictureBox1.Image = My.Resources.rock
        ElseIf (P1_val = 2) Then
            PictureBox1.Image = My.Resources.paper1
        ElseIf (P1_val = 3) Then
            PictureBox1.Image = My.Resources.scissors
        End If

        If P1_val = 1 Then
            lblp1.Text = "Rock"
        ElseIf (P1_val = 2) Then
            lblp1.Text = "Paper"
        ElseIf (P1_val = 3) Then
            lblp1.Text = "Scissor"

            ' lblp1.Text = P1_val
        End If

        If (P2_val = 1) Then
            PictureBox2.Image = My.Resources.rock
        ElseIf (P2_val = 2) Then
            PictureBox2.Image = My.Resources.paper1
        ElseIf (P2_val = 3) Then
            PictureBox2.Image = My.Resources.scissors

        End If
        If P2_val = 1 Then
            lblp2.Text = "Rock"
        ElseIf (P2_val = 2) Then
            lblp2.Text = "Paper"
        ElseIf (P2_val = 3) Then
            lblp2.Text = "Scissor"


        End If


        If P1_val = P2_val Then
            lblDisp.Text = "tie"
        ElseIf P1_val = 1 And P2_val = 2 Then
            lblDisp.Text = "Player 2 Win"
        ElseIf P1_val = 1 And P2_val = 3 Then
            lblDisp.Text = "Player 1 Win"
        ElseIf P1_val = 2 And P2_val = 1 Then
            lblDisp.Text = "Player 1 Win"
        ElseIf P1_val = 2 And P2_val = 3 Then
            lblDisp.Text = "Player 2 Win"
        ElseIf P1_val = 3 And P2_val = 2 Then
            lblDisp.Text = "Player 1 Win"
        ElseIf P1_val = 3 And P2_val = 1 Then
            lblDisp.Text = "Player 2 Win"

        End If

    End Sub

    Function Lets_Play(ByRef P1_val As Integer, ByRef P2_val As Integer, ByRef rand As Random) As Integer
        P1_val = rand.Next(3) + 1
        P2_val = rand.Next(3) + 1
        Return P1_val And P2_val

    End Function

End Class

Explanation / Answer

kindly fix these codes i am tryin to use function but it is not working thanks Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim P1_val As Integer Dim P2_val As Integer Dim rand As New Random Lets_Play(P1_val, rand) Lets_Play(P2_val, rand) 'P1_val = rand.Next(3) + 1 'P2_val = rand.Next(3) + 1 If (P1_val = 1) Then PictureBox1.Image = My.Resources.rock ElseIf (P1_val = 2) Then PictureBox1.Image = My.Resources.paper1 ElseIf (P1_val = 3) Then PictureBox1.Image = My.Resources.scissors End If If P1_val = 1 Then lblp1.Text = "Rock" ElseIf (P1_val = 2) Then lblp1.Text = "Paper" ElseIf (P1_val = 3) Then lblp1.Text = "Scissor" ' lblp1.Text = P1_val End If If (P2_val = 1) Then PictureBox2.Image = My.Resources.rock ElseIf (P2_val = 2) Then PictureBox2.Image = My.Resources.paper1 ElseIf (P2_val = 3) Then PictureBox2.Image = My.Resources.scissors End If If P2_val = 1 Then lblp2.Text = "Rock" ElseIf (P2_val = 2) Then lblp2.Text = "Paper" ElseIf (P2_val = 3) Then lblp2.Text = "Scissor" End If If P1_val = P2_val Then lblDisp.Text = "tie" ElseIf P1_val = 1 And P2_val = 2 Then lblDisp.Text = "Player 2 Win" ElseIf P1_val = 1 And P2_val = 3 Then lblDisp.Text = "Player 1 Win" ElseIf P1_val = 2 And P2_val = 1 Then lblDisp.Text = "Player 1 Win" ElseIf P1_val = 2 And P2_val = 3 Then lblDisp.Text = "Player 2 Win" ElseIf P1_val = 3 And P2_val = 2 Then lblDisp.Text = "Player 1 Win" ElseIf P1_val = 3 And P2_val = 1 Then lblDisp.Text = "Player 2 Win" End If End Sub Function Lets_Play(ByRef P1_val As Integer, ByRef P2_val As Integer, ByRef rand As Random) As Integer P1_val = rand.Next(3) + 1 P2_val = rand.Next(3) + 1 Return P1_val And P2_val End Function End Class Syntax:: Public Class Form1 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim n AsInteger Dim x, sinx, actualsinx, approx_error As Double x = TextBox1.Text n = TextBox2.Text sinx = 0 For i As Integer= 0 To n sinx = sinx + ((-1) ^ i) * ((x ^ (2 * i + 1)) / (factorial(2 * i + 1))) Next actualsinx = Math.Sin(x) approx_error = (actualsinx - sinx) / actualsinx TextBox3.Text = sinx TextBox4.Text = actualsinx TextBox5.Text = approx_error End Sub 'this function calculates the factorial Function factorial(ByVal x As Integer) As Double Dim product As Double product = 1 For i As Integer = 1 To x product = product * i Next Return product End Function Private Sub Button1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click End End Sub End Class Exploring the The Click Event Buttons have the ability to be clicked on. When you click a button, the event that is fired is the Click Event. If you were to add a new button to a form, and then double clicked it, you would see the following code stub: Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click End Sub This is a Private Subroutine. The name of the Sub is Button1_Click. The Event itself is at the end: Button1.Click. The Handles word means that this Subroutine can Handle the Click Event of Button1. Without the arguments in the round brackets, the code is this: Private Sub Button1_Click( ) Handles Button1.Click You can have this Button1_Click Sub Handle other things, too. It can Handle the Click Event of other Buttons, for example. Try this. • Start a New project • Give it the name it Events • When you new Form appears, add two Buttons to it • Double click Button1 to bring up the code • At the end of the first line for the Button, add this: Handles Button1.Click, Button2.Click Add a message box as the code for the Button. Your code window might then look like this (we've used an underscore to spread the code out over two lines) : Run your programme, and then click both of the buttons in turn. The same message box appears, regardless of which one you clicked. The reason it did so was because the Events that the Button1.Click Subroutine can Handle are at the end: the Events for Button1.Click AND Button2.Click. You can add as many Events as you want on the End. As long as the Subroutine can Handle them, the Event will happen. For example, you could create two more buttons, and then add the Click Event on the end of the first button: Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click When you click any of the four button, the code inside of the Button1_Click Subroutine will fire. However, if you double clicked button2 to try to bring up its coding window, you'd find that the cursor is flashing inside of the code for Button1_Click. Because you've attached the Click Event of button2 to the Button1 Subroutine, you can't have a separate Click Event just for Button2. This Click Event is Handled By the Subroutine called Button1_Click. Event Arguments The arguments for a Button's click event, the ones from the round brackets, are these two: sender As Object, e As EventArgs Hidden from you, is the default for these two arguments, which is ByVal rather than ByRef. So it's really this: ByVal sender As Object, ByVal e As EventArgs This sets up two variable: one called sender and one called e. Instead of sender being an integer or string variable, the type of variable set up for sender is System.Object. This stores a reference to a control (which button was clicked, for example). For the e variable, this is holding an object, too - information about the event. For a button, this information might be which Mouse Button was clicked or where the mouse pointer was on the screen. But because this is the Click Event, there's not much more information available: either the button was clicked or it wasn't. But you can use other Events available to the button. One of these is the MouseDown Event. The information for the event would be which button was clicked, where the mouse pointer was when the mouse button was held down, and something called Delta (a count of how many notches have been rotated on a mouse wheel).