I want to ask how to do this based on a task i was given, i tried but it doesn't really work can anyone tell me why? Or even better if you could show me the correct version
The Task is:
Adapt the programme so the user must enter a quality for each diamond - Each diamond has a quality that is a whole number between 1 (low quality ) and 5 (high quality).
Here is the code (Sorry if its bad i am new to VB)
Can anyone perhaps show me how to fix, i've been told the mistakes, but honestly i am not sure how to actually fix them. :(
Dim Weight As Integer
Dim userValue As Boolean = True
Dim diamonds As Integer = 0
Console.WriteLine("Enter the weight of your diamond in grams")
Weight = Console.ReadLine
Dim Quality As Integer
Dim DiamondPrice As Integer
Console.WriteLine("Enter the quality of your diamond from 1 (low quality) to 5 (high quality)")
Quality = Console.ReadLine
If Quality = "1" Then
DiamondPrice = "100"
ElseIf Quality = "2" Then
DiamondPrice = "150"
ElseIf Quality = "3" Then
DiamondPrice = "200"
ElseIf Quality = "4" Then
DiamondPrice = "250"
ElseIf Quality = "5" Then
DiamondPrice = "300"
End If
Console.ReadLine()
Console.WriteLine("This diamond costs : " & Quality * )
Console.WriteLine("Would you like to price another Y/N?")
Console.ReadLine()
While userValue
Console.WriteLine("Enter the weight of the new diamond in grams")
Weight = Console.ReadLine
Console.WriteLine("This diamond costs : " & Weight * 350)
Console.WriteLine("Would you like to price another Y/N?")
userValue = Console.ReadLine.ToUpper().Trim() = "Y"
diamonds += 1
End While
Console.WriteLine("The total number of diamonds entered is : {0}", diamonds)
Console.ReadLine()
Related
I'm trying to write a program for a vending machine. The program will ask the user to pick an option and it will calculate the change depending on the amount entered and the price of the product.
I can't get the program to calculate the change. I tried this but it doesn't work
Select Case choice
Case "1"
Console.WriteLine("enter amount")
amount = Console.ReadLine()
If amount < 0.6 Then
Console.WriteLine("not enough")
Else
change = amount - 0.6
End If
full code
Dim choice As String
Dim amount As Integer
Dim change As Integer
Sub Main(args As String())
Console.WriteLine("option 1: snickers price = 60p")
Console.WriteLine("option 2: coke price = £1")
Console.WriteLine("option 3")
Console.WriteLine("option 3")
Console.WriteLine("option 4")
Console.WriteLine("option 5")
Console.WriteLine("option 6")
Console.WriteLine("option 7")
Console.WriteLine("option 8")
Console.WriteLine("option 9")
Console.WriteLine("option 10")
choice = Console.ReadLine()
Select Case choice
Case "1"
Console.WriteLine("enter amount")
amount = Console.ReadLine()
If amount < 0.6 Then
Console.WriteLine("not enough")
Else
change = amount - 0.6
End If
Case "2"
Console.WriteLine("enter amount")
amount = Console.ReadLine()
If amount < 1 Then
Console.WriteLine("not enough")
Else
change = amount - 1
End If
End Select
Console.ReadLine()
End Sub
I see two issues in your code:
You are using Integer type for data that is decimal, so first of all you need to change the first two variables to Double
You are never showing the value that Change has; you calculate it but it is never shown on the screen. I have added a line that shows the change only if it is greater than 0.
Try something similar to the code I show below and see if you can continue from there:
Sub Main()
Dim choice As String
Dim amount As Double
Dim change As Double
Console.WriteLine("option 1: snickers price = 60p")
Console.WriteLine("option 2: coke price = £1")
Console.WriteLine("option 3")
Console.WriteLine("option 3")
Console.WriteLine("option 4")
Console.WriteLine("option 5")
Console.WriteLine("option 6")
Console.WriteLine("option 7")
Console.WriteLine("option 8")
Console.WriteLine("option 9")
Console.WriteLine("option 10")
choice = Console.ReadLine()
Select Case choice
Case "1"
Console.WriteLine("enter amount")
amount = Console.ReadLine()
If amount < 0.6 Then
Console.WriteLine("not enough")
Else
change = amount - 0.6
End If
Case "2"
Console.WriteLine("enter amount")
amount = Console.ReadLine()
If amount < 1 Then
Console.WriteLine("not enough")
Else
change = amount - 1
End If
End Select
If change > 0 then
Console.WriteLine("Change: " & change)
End If
End Sub
I'm new to coding on Visual Basics and was wondering if anyone knew why the end total cost isn't working. Currently I keep getting C as the end value
Here is the variables bit so you can see the data types I have set
Dim Height As Decimal
Dim HeightValidation As Boolean
Dim Width As Decimal
Dim WidthValidation As Boolean
Dim TotalArea As Decimal
Dim Paint As String
Dim PaintValidation As Boolean
Dim Cost As Decimal
Dim FinalCost As Decimal
Dim UndercoatValidation As Boolean
Dim Undercoat As String
And here is the last bit of the code
Do Until UndercoatValidation = True
UnderCoat = InputBox("Would you like to add an Undercoat to your purchase?")
If Undercoat = "Yes" Then
FinalCost = Cost + (TotalArea * 0.5)
UndercoatValidation = True
ElseIf Undercoat = "No" Then
FinalCost = Cost
Else
UndercoatValidation = False
End If
Loop
MsgBox("Thank you for using the Paint Calculator")
MsgBox("Your total cost is"(FormatCurrency(FinalCost)))
Thanks in advance. Btw before I added the currency bit it worked.
So the values of cost will change depending on whether or not the user wishes to have an undercoat, what type of paint they use and the area of the room.
Do Until HeightValidation = True
Height = InputBox("Enter the height of the room you are workin in: ") 'Gains users value
If (2 <= Height) AndAlso (Height <= 6) Then 'Only allows number between 2 and 6
HeightValidation = True 'breaks the loop if the requirements are met
Else
HeightValidation = False 'continues the loop if the requirements are not met
End If
Loop
Do Until WidthValidation = True
Width = InputBox("Enter the width of the room you are workin in: ") 'Gains users value
If (1 <= Width) AndAlso (Width <= 25) Then 'Only allows number between 1 and 25
WidthValidation = True 'breaks the loop if the requirements are met
Else
WidthValidation = False 'continues the loop if the requirements are not met
End If
Loop
TotalArea = Height * Width
Do Until PaintValidation = True
MsgBox("There are 3 different types of paint you could you, which are: Luxary which costs £1.75 per square metre; standard which costs £1 per square metre and economy wich costs £0.45 per square metre")
Paint = InputBox("Which type of paint will you be using to paint the walls?")
If Paint = "standard" Then
Cost = TotalArea * 1
PaintValidation = True
ElseIf Paint = "economy" Then
Cost = TotalArea * 0.45
PaintValidation = True
ElseIf Paint = "luxary" Then
Cost = TotalArea * 1.75
PaintValidation = True
Else
PaintValidation = False
End If
Loop
That's the rest of the code that works out the costs, sorry for all the code not being annotated.
MsgBox("Your total cost is"(FormatCurrency(FinalCost)))
That means "show the character at position FormatCurrency(FinalCost) from the string "Your total cost is".
Apparently the result of FormatCurrency(FinalCost) is implicitly convertible to 11, so it shows the "c".
Apparently you meant
MsgBox("Your total cost is " & FormatCurrency(FinalCost))
Please use Option Strict On to not have this kind of problems in the future.
I'm new to programming and stack overflow blogs, so hopefully I am following the 'do's and dont's' properly.
I have been given an assignment question asking me to store 5 integers in an array and to determine if they are a prime number or not.
The questions I have are as follows:
How do I store them into an integer array?
How do I make my program divide every input by every number less than the input?
The code I have written so far is this:
Sub Main()
Dim a, b, c, d, e As Integer
Dim isPrime As Boolean = True
Console.WriteLine("Please enter a value for a: ")
a = Console.ReadLine
Console.WriteLine("Please enter a value for b: ")
b = Console.ReadLine
Console.WriteLine("Please enter a value for c: ")
c = Console.ReadLine
Console.WriteLine("Please enter a value for d: ")
d = Console.ReadLine
Console.WriteLine("Please enter a value for e: ")
e = Console.ReadLine
If a Mod (a - 1) > 0 Or a = 2 And a <> 0 Then
Console.WriteLine("a is a prime number")
ElseIf a Mod (a - 1) = 0 Then
Console.WriteLine("a is not a prime number")
End If
If b Mod (b - 1) > 0 Or b = 2 And b <> 0 Then
Console.WriteLine("b is a prime number")
ElseIf b Mod (b - 1) = 0 Then
Console.WriteLine("b is not a prime number")
End If
If c Mod (c - 1) > 0 Or c = 2 And c <> 0 Then
Console.WriteLine("c is a prime number")
ElseIf c Mod (c - 1) = 0 Then
Console.WriteLine("c is not a prime number")
End If
If d Mod (d - 1) > 0 Or d = 2 And d <> 0 Then
Console.WriteLine("d is a prime number")
ElseIf d Mod (d - 1) = 0 Then
Console.WriteLine("d is not a prime number")
End If
If e Mod (e - 1) > 0 Or e = 2 And e <> 0 Then
Console.WriteLine("e is a prime number")
ElseIf e Mod (e - 1) = 0 Then
Console.WriteLine("e is not a prime number")
End If
Console.ReadKey()
End Sub
Learning all of this stuff with the prior knowledge provided has made things difficult, so any help would be greatly appreciated!
Dim a, b, c, d, e As Integer
Console.WriteLine("Please enter a value for a: ")
a = Console.ReadLine
Console.WriteLine("Please enter a value for b: ")
b = Console.ReadLine
Console.WriteLine("Please enter a value for c: ")
c = Console.ReadLine
Console.WriteLine("Please enter a value for d: ")
d = Console.ReadLine
Console.WriteLine("Please enter a value for e: ")
e = Console.ReadLine
Dim intary() As Integer = {CInt(a), CInt(b), CInt(c), CInt(d), CInt(e)}
For Each number As Integer In intary
Dim prime As Boolean = True
For x As Integer = 2 To number - 1
If number Mod x = 0 Then
prime = False
Exit For
End If
Next
If prime Then
Console.WriteLine(number.ToString & " IS a prime number")
Else
Console.WriteLine(number.ToString & " IS NOT a prime number")
End If
Next
The integer array is created in the first line. Then, the concept is you would want to iterate through each item in the array, and run a prime number test on it. So, we have a for loop that will run our prime number test on each number in the array. For each of the numbers, we divide the test-number by every number from two all the way up to the given test-number and check the mod remainder. If any of those return zero besides the number itself, then we do not have a prime number. In the example shown, I am just printing a simple line stating whether or not each number passed or failed the test...hope this helps and good luck learning your new language!
Side note, you could dim the array and add each new number as it's retrieved from the user, but you would need to redim or resize the array each time. The example I wrote just uses the input logic you already had in place. If the assignment didn't explicitly ask for an array, you'd be able to write a much cleaner solution if you were to use a list.
The basic answer to this question is to take the users inputs, and to run a mod on each of these numbers between 2 and the users input.
To the people who are starting out like me, my advice is that you don't differ from the basic idea and it will all eventually make sense!
I am making a hangman game on VB. An help is appreciated
All I have is that player 1 enters a word. The program then puts it into an array, which sorts it into letters. The player 2 (on the same computer) tries to guess the word, one letter at the time. They type in a letter and the program will go trough the array to check if there are any letter in the word. If there are it will show the letter (leaving the others blank) and leave the scoring system at 10 (I still need to put this in) If they guess wrong the letters will remain covered and it will minus 1 of the scoring system.
Module Module1
Sub Main()
Dim myword, guess As String
Dim mywordlen As Integer
Dim flag As Boolean
Console.WriteLine("Player 1, please enter a word to guess")
myword = Console.ReadLine()
mywordlen = myword.Length
Dim answer(mywordlen) As Char
For x = 0 To mywordlen - 1
answer(x) = "_"
While answer > 0 Then
Console.WriteLine("Please guess a letter")
guess = Console.ReadLine()
flag = False
For x = 0 To mywordlen - 1
If guess = myword Then {0}
answer(x) = guess
flag = True
Console.WriteLine("The answer is, {0}")
Next
Next
End Sub
Dim myword, guess As String
Dim mywordlen, x As Integer
Dim flag As Boolean
Console.WriteLine("Player 1, please enter a word to guess")
myword = Console.ReadLine()
mywordlen = myword.Length - 1
Dim answer(mywordlen), displayword(mywordlen) As Char
Dim score As Integer = 10
For x = 0 To mywordlen
answer(x) = Mid(myword, x + 1, 1)
displayword(x) = "_"
Next
While score > 0
Console.WriteLine("Please guess a letter")
guess = Console.ReadLine() 'Assumes user only types one letter
flag = False
For x = 0 To mywordlen
If guess = answer(x) Then
displayword(x) = guess
flag = True
End If
Next
If flag Then
If New String(displayword) = myword Then
Console.WriteLine("Congratulations! You won!")
Exit While
Else
Console.WriteLine("Correct! {0}", New String(displayword))
End If
Else
Console.WriteLine("Incorrect. {0}", New String(displayword))
score -= 1
End If
End While
Console.WriteLine("Game over. The word was {0}", myword)
I've been having issues trying to fix an "End of Statement Expected" Error for this program I've been working on. Everything else seems to work fine until I've started working with the Function Statement that I called Payments. In this function I'm trying to calculate the monthlyBal for every month.
The exact position that I'm getting this error is;
While monthlyBal > 0
monthlyBal = Payments(tempBalances, monthlyRate)
I've added the rest of the code below.
Module CreditCardCalculator
Sub DisplayCreditCards(ByVal cardNames() As String, ByVal cardAPRs() As Double, ByVal cardBalances() As Double)
Const _SPACE As String = " "
Dim count As Integer = 1
System.Console.WriteLine("The Order of Credit Cards To Pay Off")
System.Console.WriteLine("------------------------------------")
For pos = 0 To cardNames.Length - 1
System.Console.WriteLine("Credit Card " & count & ": ")
System.Console.WriteLine(_SPACE & "NAME: " & cardNames(pos))
System.Console.WriteLine(_SPACE & "APRs: " & cardAPRs(pos) &"%")
System.Console.WriteLine(_SPACE & "BALANCE: " & cardBalances(pos))
System.Console.WriteLine()
count = count + 1
Next
End Sub
Sub OrderofCreditCards(ByRef cardNames() As String, ByRef cardAPRs() As Double, ByRef cardBalances() As Double, ByVal SIZE as Integer)
Dim firstInput As String
Dim secondInput As String
Dim swapNames(SIZE) As String
Dim swapAPRs(SIZE) As Double
Dim swapBalances(SIZE) As Double
System.Console.WriteLine("Which Credit Card would you like to payoff first?")
firstInput = Console.ReadLine()
For pos = 0 To cardNames.Length - 1
If firstInput = cardNames(pos) Then
swapNames(0) = cardNames(pos)
swapAPRs(0) = cardAPRs(pos)
swapBalances(0) = cardBalances(pos)
Exit For
End If
Next
System.Console.WriteLine("Which Credit Card would you like to payoff second?")
secondInput = Console.ReadLine()
For pos = 0 To cardNames.Length - 1
If secondInput = cardNames(pos) Then
swapNames(1) = cardNames(pos)
swapAPRs(1) = cardAPRs(pos)
swapBalances(1) = cardBalances(pos)
Exit For
End If
Next
For pos = 0 To cardNames.Length - 1
If cardNames(pos) <> swapNames(0) Then
If cardNames(pos) <> swapNames(1) Then
swapNames(2) = cardNames(pos)
swapAPRs(2) = cardAPRs(pos)
swapBalances(2) = cardBalances(pos)
Exit For
End If
End If
Next
cardNames = swapNames
cardAPRs = swapAPRs
cardBalances = swapBalances
End Sub
Sub DisplayMenu()
System.Console.WriteLine("CREDIT CARD CALCULATOR MENU")
System.Console.WriteLine("===========================")
System.Console.WriteLine("OPTION 1. Display Total Number Of Payments Required To Pay Off Each
Card. ")
System.Console.WriteLine()
System.Console.WriteLine("OPTION 2. Display The Number Of Years, Or Months To Pay Off Each Card.
")
System.Console.WriteLine()
System.Console.WriteLine("OPTION 3. Display The Balance To Payoff Each Card and Total Amount To
Payoff All Cards Combined. ")
System.Console.WriteLine()
System.Console.WriteLine("OPTION 4. Exit The Program. ")
System.Console.WriteLine()
System.Console.WriteLine
("=============================================================================")
System.Console.WriteLine("Instructions: Type The Number That Is Next To The Option You Want To
Execute. ")
End Sub
Function Payments(ByVal tempBalances As Double, ByVal monthlyRate As Double) As Double
Const ISSUECHARGE As Integer = 3
Dim avgMonthlyBal As Double
Dim interest As Double
Dim minimumPayment As Double
avgMonthlyBal = tempBalances
interest = monthlyRate
avgMonthlyBal = avgMonthlyBal + interest
minimumPayment = avgMonthlyBal * ISSUECHARGE
avgMonthlyBal = avgMonthlyBal - minimumPayment
Return avgMonthlyBal
End Function
Sub Main()
Const MAX_SIZE AS Integer = 2
Const BILLPERIOD As Integer = 30
Const MONTHSINYEAR As Integer = 12
Dim creditCards(MAX_SIZE) As String
creditCards(0) = "Discover"
creditCards(1) = "Visa"
creditCards(2) = "Master Card"
Dim creditCardAPRs(MAX_SIZE) As Double
creditCardAPRs(0) = 12.99
creditCardAPRs(1) = 7.5
creditCardAPRs(2) = 18.9
Dim creditCardBalances(MAX_SIZE) As Double
creditCardBalances(0) = 300
creditCardBalances(1) = 400
creditCardBalances(2) = 500
Dim myInput As String
Dim optionNum As String
Dim tempBalances As Double
Dim monthlyRate As Double
Dim numberofDays As Integer
Dim monthlyBal As Double
DisplayCreditCards(creditCards, creditCardAPRs, creditCardBalances)
System.Console.WriteLine("Would you like to adjust the order of the Credit Card?")
System.Console.WriteLine()
System.Console.WriteLine("If Yes, type 'Y' --------------------- If No, type 'N'")
myInput = Console.ReadLine()
If myInput = "Y" Then
OrderofCreditCards(creditCards, creditCardAPRs, creditCardBalances, MAX_SIZE)
End If
System.Console.WriteLine()
Console.Clear()
DisplayCreditCards(creditCards, creditCardAPRs, creditCardBalances)
System.Console.WriteLine()
DisplayMenu()
optionNum = Console.ReadLine()
numberOfDays = 30
Select Case optionNum
Case "1"
For pos = 0 To creditCards.Length - 1
tempBalances = creditCardBalances(pos) * numberOfDays / BILLPERIOD
monthlyRate = creditCardAPRs(pos) / MONTHSINYEAR
monthlyBal = creditCardBalances(pos)
While monthlyBal > 0
monthlyBal = Payments(tempBalances, monthlyRate)
System.Console.WriteLine(monthlyBal)
End While
Next
Case "2"
System.Console.WriteLine("Case 2")
Case "3"
System.Console.WriteLine("Case 3")
Case "4"
System.Console.WriteLine("Exiting The Program... ")
Console.Read()
Case Else
System.Console.WriteLine("Error: Not a valid option from the menu. ")
System.Console.WriteLine("Exiting The Program... ")
End Select
End Sub
End Module
It's either something small and I haven't spotted it yet or, I'm not working with functions correctly since the compiler seems to point to it. Like, I said everything else seems to be working fine and compiled correctly, until I added the "Function Payments Statement" and the stuff inside the "Case 1 Statment".
One thing that seems a little suspicious is that you have a number of (apparently) spaces at the end of the while line. I'd start by getting rid of those and trying again.
It may be that there's some funny characters in there that just got pasted into Stack Overflow as spaces, and these may be causing grief to the compiler.
It's a long shot since you have a few other lines like that but it's the only strangeness I can see at or around that line.
Once I fixed DisplayMenu() it all compiled just fine:
Sub DisplayMenu()
System.Console.WriteLine("CREDIT CARD CALCULATOR MENU")
System.Console.WriteLine("===========================")
System.Console.WriteLine("OPTION 1. Display Total Number Of Payments Required To Pay Off Each Card. ")
System.Console.WriteLine()
System.Console.WriteLine("OPTION 2. Display The Number Of Years, Or Months To Pay Off Each Card. ")
System.Console.WriteLine()
System.Console.WriteLine("OPTION 3. Display The Balance To Payoff Each Card and Total Amount To Payoff All Cards Combined. ")
System.Console.WriteLine()
System.Console.WriteLine("OPTION 4. Exit The Program. ")
System.Console.WriteLine()
System.Console.WriteLine("=============================================================================")
System.Console.WriteLine("Instructions: Type The Number That Is Next To The Option You Want To Execute. ")
End Sub
In VB, you can not split strings in multiple lines like this
'This will give an error
System.Console.WriteLine("OPTION 3. Display The Balance To Amount To
Payoff Each Card and Total Payoff All Cards Combined. ")
Even if you remove the blank line in the middle, this will also give you an error
'This will also give an error
System.Console.WriteLine("OPTION 3. Display The Balance To Amount To
Payoff Each Card and Total Payoff All Cards Combined. ")
Here is what you can do if you must break long lines of code in VB:
'This is acceptable
System.Console.WriteLine("OPTION 3. Display The Balance To Amount To " & _
"Payoff Each Card and Total Payoff All Cards Combined. ")
Or alternatively, (if you must have blank lines in between your code) you can type it like this:
'This is also acceptable
System.Console.WriteLine("OPTION 3. Display The Balance To Amount To " & _
"" & _
"Payoff Each Card and Total Payoff All Cards Combined. ")
con.ConnectionString = "Data Source=ADMIN-PC\SQLEXPRESS;Initial Catalog=" BILLING SYSTEM";Integrated Security=True"
This Expression will have error BC30205 End of statement expected.