Can someone explain why the below code won't work? It's supposed to find the average in a group of 15 numbers, then assign a letter grade to them. From what I can tell, the problem is in the If statement, although I can't seem to find what it is that causes the problem.
Module Module1
Sub Main()
Dim Input1 As Integer
Dim Input2 As Integer
Dim Input3 As Integer
Dim Input4 As Integer
Dim Input5 As Integer
Dim Input6 As Integer
Dim Input7 As Integer
Dim Input8 As Integer
Dim Input9 As Integer
Dim Input10 As Integer
Dim Input11 As Integer
Dim Input12 As Integer
Dim Input13 As Integer
Dim Input14 As Integer
Dim Input15 As Integer
Dim Adder As Integer
Dim Divider As Integer
Console.WriteLine("Please enter number 1/15")
Input1 = Console.ReadLine()
Console.WriteLine("Please enter number 2/15")
Input2 = Console.ReadLine()
Console.WriteLine("Please enter number 3/15")
Input3 = Console.ReadLine()
Console.WriteLine("Please enter number 4/15")
Input4 = Console.ReadLine()
Console.WriteLine("Please enter number 5/15")
Input5 = Console.ReadLine()
Console.WriteLine("Please enter number 6/15")
Input6 = Console.ReadLine()
Console.WriteLine("Please enter number 7/15")
Input7 = Console.ReadLine()
Console.WriteLine("Please enter number 8/15")
Input8 = Console.ReadLine()
Console.WriteLine("Please enter number 9/15")
Input9 = Console.ReadLine()
Console.WriteLine("Please enter number 10/15")
Input10 = Console.ReadLine()
Console.WriteLine("Please enter number 11/15")
Input11 = Console.ReadLine()
Console.WriteLine("Please enter number 12/15")
Input12 = Console.ReadLine()
Console.WriteLine("Please enter number 13/15")
Input13 = Console.ReadLine()
Console.WriteLine("Please enter number 14/15")
Input14 = Console.ReadLine()
Console.WriteLine("Please enter number 15/15")
Input15 = Console.ReadLine()
Adder = (Input1) + (Input2) + (Input3) + (Input4) + (Input5) + (Input6) + (Input7) + (Input8) + (Input9) + (Input10) + (Input11) + (Input12) + (Input13) + (Input14) + (Input15)
Divider = Adder / 15
If 100 >= Divider >= 90 Then
Console.WriteLine("Your average Grade is an A, and your average in numbers is {0}", Divider)
ElseIf 89 >= Divider >= 80 Then
Console.WriteLine("Your average Grade is a B, and your average in numbers is {0}", Divider)
ElseIf 79 >= Divider >= 70 Then
Console.WriteLine("Your average Grade is a C, and your average in numbers is {0}", Divider)
ElseIf 69 >= Divider >= 60 Then
Console.WriteLine("Your average Grade is a D, and your average in numbers is {0}", Divider)
ElseIf 59 >= Divider Then
Console.WriteLine("Your average Grade is an F, and your average in numbers is {0}", Divider)
End If
Console.ReadLine()
End Sub
End Module
You can't do the following:
If 100 >= Divider >= 90
I believe that is interpreted to be something like
If (100 >= Divider) >= 90
Where it is comparing a boolean converted to a integer.
To get a check for that range you would need to do:
If (100 >= Divider) AndAlso (Divider >= 90)
In short you have bad syntax with your If Check
As an added note, I would reccomend taking advantage of the strength of conditional logic by skipping the range checks and doing something like:
If Divider >= 90 Then
Console.WriteLine("Your average Grade is an A, and your average in numbers is {0}", Divider)
ElseIf Divider >= 80 Then
Console.WriteLine("Your average Grade is a B, and your average in numbers is {0}", Divider)
ElseIf Divider >= 70 Then
Console.WriteLine("Your average Grade is a C, and your average in numbers is {0}", Divider)
ElseIf Divider >= 60 Then
Console.WriteLine("Your average Grade is a D, and your average in numbers is {0}", Divider)
Else
Console.WriteLine("Your average Grade is an F, and your average in numbers is {0}", Divider)
End If
You made an error in reasoning.
The Divider in your case has a Maximum value of 15. so it's obviously always an F Grade.
If Adder >= 90 Then
Console.WriteLine("Your average Grade is an A, and your average in numbers is {0}", Divider)
ElseIf Adder >= 80 Then
Console.WriteLine("Your average Grade is a B, and your average in numbers is {0}", Divider)
ElseIf Adder >= 70 Then
Console.WriteLine("Your average Grade is a C, and your average in numbers is {0}", Divider)
ElseIf Adder >= 60 Then
Console.WriteLine("Your average Grade is a D, and your average in numbers is {0}", Divider)
ElseIf Adder < 60 Then
Console.WriteLine("Your average Grade is an F, and your average in numbers is {0}", Divider)
End If
Here you go. I just switched the Divider with Adder and erased some non-essentiel stuff.
Hope this is what you are looking for.
Related
After running through the following program, lownum stays 0. It makes complete sense if you do not enter any negative numbers. However, what are my options to get the lowest number if all positive numbers are entered.
Sub Main()
Declaring variables
Dim number1 As Integer = 0
Dim number2 As Integer = 0
Dim lownum As Integer
Dim highnum As Integer
'For statement to run loop 10 times
For counter As Integer = 1 To 10
'Prompting user to enter two numbers
Console.Write("Enter the first number: ")
number1 = Console.ReadLine
Console.Write("Enter the second number: ")
number2 = Console.ReadLine
'If statements to determine and keep track of highest and lowest number
If number1 > number2 Then
Console.WriteLine("Number 1 is larger " & number1)
ElseIf number2 > number1 Then
Console.WriteLine("Number 2 is larger " & number2)
Else
Console.WriteLine("The two numbers are equal: " & number1 & " " & number2)
End If
If number1 > highnum Then
highnum = number1
End If
If number1 < lownum Then
lownum = number1
End If
If number2 > highnum Then
highnum = number2
End If
If number2 < lownum Then
lownum = number2
End If
Next
'Displaying highest and lowest numbers
Console.WriteLine("The highest number entered was " & highnum)
Console.WriteLine("The lowest number entered was " & lownum)
'Prompting user for input to continue
Console.WriteLine("Press any key to continue: ")
Console.ReadKey()
End Sub
Just check your counter variable. If it is 1, then set low and high to your values appropriately. If it is greater than 1, then do the comparisons:
For counter As Integer = 1 To 10
' ... other code ...
If counter = 1 Then
lownum = Math.Min(number1, number2)
highnum = Math.Max(number1, number2)
Else
lownum = Math.Min(lownum, Math.Min(number1, number2))
highnum = Math.Max(highnum, Math.Max(number1, number2))
End If
Next
You testing for values being less or greater then your two starting variables of 0.
If you don't give the min/max values and don't enter values < 0 or > 0 then the values of Min/Max are NEVER set.
So you want to set these to the max allowable and min allowable values.
Change you variable declares to this with initialization of the min/max and your code should work just fine.
Dim number1 As Integer = 0
Dim number2 As Integer = 0
Dim lownum As Integer = Integer.MaxValue
Dim highnum As Integer = Integer.MinValue
Console.WriteLine("Please Enter the number")
Dim number As Integer = Console.ReadLine()
If (number=< 40) Then
number = number* 10
ElseIf (number=< 150) Then
number= number* 15
Else
number= number* 26
End If
Console.WriteLine(number)
Dim total As Integer
Dim vALUE As Integer
Console.WriteLine("Please, type 1 for x . Type 2 for y. Type 3 z")
vALUE = Console.ReadLine()
If vALUE = 1 Then
Console.WriteLine("x")
total = number* (106 / 100)
ElseIf vALUE = 2
Console.WriteLine("y")
total = number* (112 / 100)
ElseIf uSERvALUE = 3
Console.WriteLine("z")
total = number* (116 / 100)
Else
Console.WriteLine("Sorry please re-enter the value")
vALUE = Nothing
End If
End While
Please tell me how to repeat the if condition. In the else line I have displayed to the user to re-enter the value. Therefore I need to repeat the if condition until the value is 1 or 2 or 3.Please explain how to do I'm a newbie.
You can use infinite While-loop ,and put "Exit While" on the end of conditional statement you agreed , so there are few ways out from infinite loop.
For example , on your code:
Console.WriteLine("Please Enter the number")
Dim number As Integer = Console.ReadLine()
If (number=< 40) Then
number = number* 10
ElseIf (number=< 150) Then
number= number* 15
Else
number= number* 26
End If
Console.WriteLine(number)
Dim total As Integer
Dim vALUE As Integer
'infinite loop until user input 1,2 or 3
While True
Console.WriteLine("Please, type 1 for x . Type 2 for y. Type 3 z")
vALUE = Console.ReadLine()
If vALUE = 1 Then
Console.WriteLine("x")
total = number* (106 / 100)
Exit While 'condition matched , break from While
ElseIf vALUE = 2
Console.WriteLine("y")
total = number* (112 / 100)
Exit While 'condition matched , break from While
ElseIf uSERvALUE = 3
Console.WriteLine("z")
total = number* (116 / 100)
Exit While 'condition matched , break from While
Else
Console.WriteLine("Sorry please re-enter the value")
vALUE = Nothing
End If
End While
'do further more you need
Module Module1
Sub Main()
Dim N, d, m, y, T As Integer
Console.Write("Enter the date(DD): ")
Console.ReadLine()
Console.Write("Enter the month(MM): ")
Console.ReadLine()
Console.Write("Enter the year(YY): ")
Console.ReadLine()
N = d + 2 * m + (3 * (m + 1) / 5) + y + (y / 4) - (y / 100) + (y / 400) + 2
T = N Mod 7 - 1
If T = 1 Then
Console.Write("The day is Monday")
Console.ReadLine()
ElseIf T = 2 Then
Console.Write("The day is Tuesday")
Console.ReadLine()
ElseIf T = 3 Then
Console.Write("The day is Wednesday")
Console.ReadLine()
ElseIf T = 4 Then
Console.Write("The day is Thursday")
Console.ReadLine()
ElseIf T = 5 Then
Console.Write("The day is Friday")
Console.ReadLine()
ElseIf T = 6 Then
Console.Write("The day is Saturday")
Console.ReadLine()
ElseIf T = 7 Then
Console.Write("The day is Sunday")
Console.ReadLine()
End If
End Sub
End Module
Depending on the formula, i suppose that it should work but it's not. It's not giving me the right day. Maybe because we also need to calculate the leap year in it? I need simple vb net code.
To me the formula you showed is unnecessary complex as there is a built in way to achieve what you want.
What about just:
Dim dt As New DateTime(2003, 5, 1)
Console.WriteLine("The day of the week for {0:d} is {1}.", dt, dt.DayOfWeek)
I am taking an online Visual Basic programming 1 course with very little instruction. I have spent hours on every assignment and can usually find help by looking things up and testing. I have not been able to find help for this (or I find it in another language and I don't know how to translate into VB console). I am asking the user to input test scores. My problem is I don't know how many scores that they will input. I need this number so that I can calculate an average and return a grade.
Module Module1
Sub Main()
Dim testScore As Integer = 1
Dim scoreSum As Integer = 0
Dim numberOfScore As Integer
Dim average As Integer
Console.WriteLine("Enter all the test scores. When you are done enter 0. ")
Do While (testScore <> 0)
testScore = Console.ReadLine()
scoreSum = scoreSum + testScore
numberOfScore += testScore
average = scoreSum / numberOfScore
Console.Write(scoreSum.ToString)
Loop
Console.WriteLine("The average is " + (scoreSum / numberOfScore).ToString())
If (average >= 90) Then
Console.WriteLine("Your Grade is A ")
ElseIf (average >= 80) Then
Console.WriteLine("Your Grade is B ")
ElseIf (average >= 70) Then
Console.WriteLine("Your Grade is C ")
ElseIf (average >= 60) Then
Console.WriteLine("Your Grade is D ")
Else
Console.WriteLine("Your Grade is F. " & "You will have to repeat this course. ")
End If
Console.ReadLine()
End Sub
End Module
You need to increment numberOfScore by 1 instead of by testScore. Average would be the sum over number of scores.
Sub Main()
Dim testScore As Integer = 1
Dim scoreSum As Integer = 0
Dim numberOfScore As Integer
Dim average As Integer
Console.WriteLine("Enter all the test scores. When you are done enter 0. ")
testScore = Console.ReadLine()
Do
scoreSum = scoreSum + testScore
numberOfScore += 1
average = scoreSum / numberOfScore
Console.Write(scoreSum.ToString)
testScore = Console.ReadLine()
Loop While (testScore <> 0)
Console.WriteLine("The average is " + (scoreSum / numberOfScore).ToString())
If (average >= 90) Then
Console.WriteLine("Your Grade is A ")
ElseIf (average >= 80) Then
Console.WriteLine("Your Grade is B ")
ElseIf (average >= 70) Then
Console.WriteLine("Your Grade is C ")
ElseIf (average >= 60) Then
Console.WriteLine("Your Grade is D ")
Else
Console.WriteLine("Your Grade is F. " & "You will have to repeat this course. ")
End If
Console.ReadLine()
End Sub
In my program to calculate grades, I need to find the percentage needed on the final to get a certain grade.
I calculate my variable PNA which is points needed for an A.
I do 900 - GSF (which is grade so far)
for a perfect score, I come back with 50 points needed.
Then, I do my variable PeNA, which is percentage needed on the final.
So I did multiple things:
PeNA = PNA / 150
and
PeNA = (900-GSF) / 150
Both results yielded 0.00%
I know that the result should be 33.33% but to no avail.
My question is how to format that PeNA variable and Statement to come back with the correct percent needed.
I think I might have a logic error somewhere in here. But I am not sure as to what is wrong.
Once I figure this out the project should be done.
Here is my code.
Sub Main()
Dim TG1 As Integer = -1
While TG1 < 0 Or TG1 > 200
Console.WriteLine("Please enter your grade for project #1 <0-200>: ")
TG1 = Console.ReadLine()
End While
Dim TG2 As Integer = -1
While TG2 < 0 Or TG2 > 200
Console.WriteLine("Please enter your grade for project #2 <0-200>: ")
TG2 = Console.ReadLine()
End While
Dim TG3 As Integer = -1
While TG3 < 0 Or TG3 > 200
Console.WriteLine("Please enter your grade for project #3 <0-200>: ")
TG3 = Console.ReadLine()
End While
Dim MT1 As Integer = -1
While MT1 < 0 Or MT1 > 150
Console.WriteLine("Please enter your grade on the midterm <0-150>: ")
MT1 = Console.ReadLine()
End While
Dim AB1 As Integer = -1
While AB1 < 0 Or AB1 > 30
Console.WriteLine("Please enter how much you were absent <0-30>: ")
AB1 = Console.ReadLine()
End While
Dim AB2 As Integer
If AB1 <= 2 Then AB2 = 100
If AB1 > 2 AndAlso AB1 < 8 Then AB2 = 100 - ((AB1 - 2) * 20)
If AB1 > 7 Then AB2 = 0
Dim GSF As Integer
GSF = TG1 + TG2 + TG3 + MT1 + AB2
Dim PNA As Integer
PNA = 900 - GSF
Dim PeNA As Integer
PeNA = (900 - GSF) / 150
Console.WriteLine("------------------------------------------------")
Console.WriteLine("--GRADE SUMMARY--")
Console.WriteLine("Project #1 : {0}", TG1)
Console.WriteLine("Project #2 : {0}", TG2)
Console.WriteLine("Project #3 : {0}", TG3)
Console.WriteLine("Midterm Exam : {0}", MT1)
Console.WriteLine("Participation : {0}", AB2)
Console.WriteLine("-------------------------------------------------")
Console.WriteLine("Total grade so far: {0}", GSF)
Console.WriteLine("")
Console.WriteLine("{0} {1} {2}", "Desired grade".PadRight(20), "Points needed".PadRight(20), "Percentage needed on final")
Console.WriteLine("{0} {1} {2}", "A".PadRight(20), PNA.ToString.PadRight(20), FormatPercent(PeNA).ToString.PadRight(20))
'A - 900-1000
'B - 800-899
'C - 700-799
'D - 600-699
Dim PNA As Integer
PNA = 900 - GSF
Dim PeNA As Integer
PeNA = (900 - GSF) / 150
PeNA should be declared SINGLE, DOUBLE or DECIMAL if you want it to store a fraction