need help to make the input of 101 show as invalid instead of A* as an output on a console app from visual studio code
Imports System
Module Program
Sub Main(args As String())
Dim mark As Byte
Dim grade As String
Console.WriteLine("enter a mark out of 100")
mark = Console.ReadLine
If mark <= 39 Then
grade = "u"
ElseIf mark <= 49 Then
grade = ("e")
ElseIf mark <= 59 Then
grade = ("d")
ElseIf mark <= 69 Then
grade = ("C")
ElseIf mark <= 79 Then
grade = ("B")
ElseIf mark <= 89 Then
grade = ("A")
ElseIf mark >= 99 Then
grade = ("A*")
Else
grade = ("invailid")
End If
Console.WriteLine()
Console.WriteLine("Grade is :" & grade)
Console.ReadLine()
End Sub
End Module
Related
Make a program that reads a positive integer no greater than 10, and prints a triangle of numbers as follows:
If the number read were 5, then it should print:
1
22
333
4444
55555
The program must reread another number until the number entered is greater than 10.
I have tried this, but I don't know if it is correct:
Dim num As Integer
Dim i As Integer
num = InputBox(" enter a number")
For i = 1 To num Step 1
If i = 1 Then
ListBox1.Items.Add(1)
ElseIf i = 2 Then
ListBox1.Items.Add(22)
ElseIf i = 3 Then
ListBox1.Items.Add(333)
ElseIf i = 4 Then
ListBox1.Items.Add(4444)
ElseIf i = 5 Then
ListBox1.Items.Add(55555)
ElseIf i = 6 Then
ListBox1.Items.Add(666666)
ElseIf i = 7 Then
ListBox1.Items.Add(7777777)
ElseIf i = 8 Then
ListBox1.Items.Add(88888888)
ElseIf i = 9 Then
ListBox1.Items.Add(999999999)
ElseIf i = 10 Then
ListBox1.Items.Add(1010101010101010101010)
End If
Next
I'm creating a Diabetes management algorithm, and I'm trying to find a way for the user's entered time blocks to be maintained at 4 digits
I've been searching on google, but all I have been able to find is how to check the length of a variable, which I already know how to do.
Sub timeBlocks()
Dim file As String = "C:\Users\Connor\Documents\Visual Studio 2017\Projects\meterCodeMaybe\TIMEBLOCKS.txt"
Dim blockNum As Integer
Console.WriteLine("Please be sure to enter times as a 24 hour value, rather than 12 hour, otherwise the input will not be handled.")
Console.Write("Please enter the amount of time blocks you require for your day: ")
blockNum = Console.ReadLine()
Dim timeA(blockNum - 1) As Integer
Dim timeB(blockNum - 1) As Integer
Dim sensitivity(blockNum - 1) As Integer
Dim ratio(blockNum - 1) As Integer
For i = 0 To (blockNum - 1)
Console.WriteLine("Please enter the start time of your time block")
timeA(i) = Console.ReadLine()
Console.WriteLine("Please enter the end time of your time block")
timeB(i) = Console.ReadLine()
Console.WriteLine("Please enter the ratio for this time block (Enter the amount of carbs that go into 1 unit of insulin)")
ratio(i) = Console.ReadLine()
Console.WriteLine("Please enter the insulin sensitivity for this time block
(amount of blood glucose (mmol/L) that is reduced by 1 unit of insulin.)")
sensitivity(i) = Console.ReadLine()
FileOpen(1, file, OpenMode.Append)
PrintLine(1, Convert.ToString(timeA(i)) + "-" + Convert.ToString(timeB(i)) + " 1:" + Convert.ToString(ratio(i)) + " Insulin Sensitivity:" + Convert.ToString(sensitivity(i)) + " per mmol/L")
FileClose(1)
Next
End Sub
Basically, I want the user to be able to enter a 4 digit number for their time block, to match a 24 hr time, so if they enter 0000, it is displayed as this, however, it removes all previous 0's and sets it to just 0.
Perhaps pad the number with 4 leading 0's:
Right(String(digits, "0") & timeA(i), 4)
Or as an alternative, store the value as a string so that it can be printed out in its original form.
I have written a Function to get a 24 hours format time from user, I hope it would help:
Public Function Read24HFormatTime() As String
Dim str As String = String.Empty
While True
Dim c As Char = Console.ReadKey(True).KeyChar
If c = vbCr Then Exit While
If c = vbBack Then
If str <> "" Then
str = str.Substring(0, str.Length - 1)
Console.Write(vbBack & " " & vbBack)
End If
ElseIf str.Length < 5 Then
If Char.IsDigit(c) OrElse c = ":" Then
If str.Length = 0 Then
' allow 0, 1 or 2 only
If c = "0" OrElse c = "1" OrElse c = "2" Then
Console.Write(c)
str += c
End If
ElseIf str.Length = 1 Then
If str = "0" Then
'allow 1 to 9
If c <> ":" Then
If CInt(c.ToString) >= 1 AndAlso CInt(c.ToString) <= 9 Then
Console.Write(c)
str += c
End If
End If
ElseIf str = "1" Then
'allow 0 to 9
If c <> ":" Then
If CInt(c.ToString) >= 0 AndAlso CInt(c.ToString) <= 9 Then
Console.Write(c)
str += c
End If
End If
ElseIf str = "2" Then
'allow 0 to 4
If c <> ":" Then
If CInt(c.ToString) >= 0 AndAlso CInt(c.ToString) <= 4 Then
Console.Write(c)
str += c
End If
End If
End If
ElseIf str.Length = 2 Then
'allow ":" only
If c = ":" Then
Console.Write(c)
str += c
End If
ElseIf str.Length = 3 Then
If str = "24:" Then
'allow 0 only
If c = "0" Then
Console.Write(c)
str += c
End If
Else
'allow 0 to 5
If c <> ":" Then
If CInt(c.ToString) >= 0 AndAlso CInt(c.ToString) <= 5 Then
Console.Write(c)
str += c
End If
End If
End If
ElseIf str.Length = 4 Then
If str.Substring(0, 3) = "24:" Then
'allow 0 only
If c = "0" Then
Console.Write(c)
str += c
End If
Else
'allow 0 to 9
If c <> ":" Then
If CInt(c.ToString) >= 0 AndAlso CInt(c.ToString) <= 9 Then
Console.Write(c)
str += c
End If
End If
End If
End If
End If
End If
End While
Return str
End Function
The user can only enter time like 23:59 08:15 13:10 and he couldn't enter formats like 35:10 90:00 25:13 10:61
This is a sample code to show you how to use it:
Dim myTime = DateTime.Parse(Read24HFormatTime())
Dim name = "Emplyee"
Console.WriteLine($"{vbCrLf}Hello, {name}, at {myTime:t}")
Console.ReadKey(True)
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
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
I'm working on an assignment where the program needs to calculate and display the total cost for shipping a package, based on weight and whether it is being shipped to Continental U.S., Alaska, or Hawaii. When I click the Calculate button, though, the label that is supposed to display the total is left blank. I've looked through this and tried placing the calculation in different parts/at the end of the "If" statements. Here's what I have thus far, any help would be appreciated:
Dim decWeight As Decimal
Dim decTotalCost As Decimal
Dim decDestination As Decimal
Dim decRate As Decimal
If IsNumeric(txtWeight.Text) Then
decWeight = Convert.ToDecimal(txtWeight.Text)
If decWeight <= 30 > 0 Then
If radContinental.Checked Then
decDestination = 1
ElseIf radHawaii.Checked Then
decDestination = 1.2
ElseIf radAlaska.Checked Then
decDestination = 1.26
End If
If decWeight <= 2 Then
decRate = 3.69
ElseIf decWeight <= 4 > 2 Then
decRate = 4.86
ElseIf decWeight <= 6 > 4 Then
decRate = 5.63
ElseIf decWeight <= 8 > 6 Then
decRate = 5.98
ElseIf decWeight <= 10 > 8 Then
decRate = 6.28
ElseIf decWeight <= 30 > 10 Then
decRate = 15.72
End If
decTotalCost = decRate * decDestination
lblTotalCost.Text = decTotalCost.ToString("C")
ElseIf decWeight <= 0 Then
MsgBox("Please Enter a Positive Weight.", , "Input Error")
ElseIf decWeight > 30 Then
MsgBox("You Have Entered " & decWeight.ToString() & ". Please Enter a Weight Under 30 Pounds", , "Input Error")
End If
ElseIf txtWeight.Text = "" Then
MsgBox("Please Enter the Weight", , "Input Error")
Else : MsgBox("Please Enter a Number", , "Input Error")
End If
You should try this if statement: If decWeight <= 30 and decWeight > 0 Then
This will check if the decWeight is less than or equal to 30 and make sure that it is 'non-zero' Hope this helps :-)