If...Then...Else Visual Basic 2012

842 views Asked by At

I'm trying to create an app in Visual Basic 2012, I am struggling to get it to meet the parameters. What am I doing wrong? Every time I try to add Else it says there must be an If preceding it. The Problem:

Write a program for a college’s admissions office. Create variables that store a numeric high school grade point average and an admission test score (two different criteria can be used using an “and” for example if gpa >=3.6 and score >=60 then ) Print the message “Accept” if the student has any of the following:

  • A grade point average of 3.6 or above and an admission test score of at least 60
  • A grade point average of 3.0 or above and an admission test score of at least 70
  • A grade point average of 2.6 or above and an admission test score of at least 80
  • A grade point average of 2.0 or above and an admission test score of at least 90

If the student does not meet any of the qualifications, print “Reject”. The picture below will give you a reference how your interface should look.

    If txtGPA.Text >= 3.6 And txtAdmissionTestScore.Text >= 60 Then txtAdmissionResult.Text = "Accept"
    If txtGPA.Text >= 3.6 And txtAdmissionTestScore.Text <= 59 Then txtAdmissionResult.Text = "Reject"

    If txtGPA.Text >= 3.0 And txtAdmissionTestScore.Text >= 70 Then txtAdmissionResult.Text = "Accept"
    If txtGPA.Text >= 3.0 And txtAdmissionTestScore.Text <= 69 Then txtAdmissionResult.Text = "Reject"

    If txtGPA.Text >= 2.6 And txtAdmissionTestScore.Text >= 80 Then txtAdmissionResult.Text = "Accept"
    If txtGPA.Text >= 2.6 And txtAdmissionTestScore.Text <= 79 Then txtAdmissionResult.Text = "Reject"

    If txtGPA.Text >= 2.0 And txtAdmissionTestScore.Text >= 90 Then txtAdmissionResult.Text = "Accept"
    If txtGPA.Text >= 2.0 And txtAdmissionTestScore.Text <= 89 Then txtAdmissionResult.Text = "Reject"

    If txtGPA.Text <= 1.9 And txtAdmissionTestScore.Text <= 59 Then txtAdmissionResult.Text = "Reject"
3

There are 3 answers

2
Mark Hall On

You need to do something like this. Note I am checking for the common condition then checking sub conditions, the sub conditions are either accept or reject. Therefore you only need to test for one.

If txtGPA.Text >= 3.6 Then 
    If txtAdmissionTestScore.Text >= 60 Then 
        txtAdmissionResult.Text = "Accept"
    Else
        txtAdmissionResult.Text = "Reject"
    End If
ElseIf txtGPA.Text >= 3.0 Then   
    If txtAdmissionTestScore.Text >= 70 Then 
         txtAdmissionResult.Text = "Accept"
     Else
         txtAdmissionResult.Text = "Reject"
     Endif
ElseIf ...

End If
1
Idle_Mind On

This is simply Douglas Barbin's code reformatted for readability (this is pretty subjective obviously!), and the last condition removed since you can't be admitted with less than a 2.0 GPA. I've also added in checks to make sure the entered GPA and Test Score were a valid double and integer respectively:

    Dim GPA As Double
    Dim AdmissionTestScore As Integer
    If Double.TryParse(txtGPA.Text, GPA) AndAlso Integer.TryParse(txtAdmissionTestScore.Text, AdmissionTestScore) Then
        Dim shouldAccept As Boolean = _
            (GPA >= 3.6 AndAlso AdmissionTestScore >= 60) OrElse _
            (GPA >= 3.0 AndAlso AdmissionTestScore >= 70) OrElse _
            (GPA >= 2.6 AndAlso AdmissionTestScore >= 80) OrElse _
            (GPA >= 2.0 AndAlso AdmissionTestScore >= 90)
        txtAdmissionResult.Text = If(shouldAccept, "Accept", "Reject")
    Else
        MessageBox.Show("Invalid GPA and/or Admission Test Score!")
    End If
0
Douglas Barbin On
Dim shouldAccept As Boolean = (txtGPA.Text >= 3.6 AndAlso txtAdmissionTestScore.Text >= 60) OrElse _ 
    (txtGPA.Text >= 3.0 AndAlso txtAdmissionTestScore.Text >= 70) OrElse _
    (txtGPA.Text >= 2.6 AndAlso txtAdmissionTestScore.Text >= 80) OrElse _
    (txtGPA.Text >= 2.0 AndAlso txtAdmissionTestScore.Text >= 90)

txtAdmissionResult.Text = If(shouldAccept, "Accept", "Reject")