The output I am getting is is not what I am suppose to be getting when you type in 50 dollars i should get 58 bars and 2 coupons but when i run it i am getting 57 with 7 coupons left. Thank you.
Module Module1
Sub Main()
' declare variable
Dim amountDollar As Integer
Dim totalCoupons As Integer
Dim leftOverCoupons As Integer
Dim numberofChocolate As Integer
Dim freeChocolate As Integer
Dim totalchocolate As Integer
Console.WriteLine("Please enter your amount here")
amountDollar = CInt(Console.ReadLine())
numberofChocolate = amountDollar
freeChocolate = CInt(numberofChocolate / 7)
totalchocolate = numberofChocolate + freeChocolate
Console.WriteLine("Total number of chocolate: " & totalchocolate)
leftOverCoupons = freeChocolate Mod numberofChocolate
Console.WriteLine("Leftover Coupons: " & leftOverCoupons)
End Sub
End Module
ok this is the new one but i can do it will the mod and the divison why is that
Sub Main()
' This program will calculate the number of chocolate bars you can buy or redeem by coupons for an input number
' of dollars. Each bar costs one dollar and there is one coupon in each bar. It takes 7 coupons to receive one
' additional chocolate bar.
Dim dollars, numberOfBars, couponCount As Integer
' Prompt user to enter amount of money they have to spend
Console.WriteLine("Would you like to buy candy bars")
Console.WriteLine("Yes")
Console.WriteLine("No")
Console.WriteLine("Please enter your amount here")
dollars = Convert.ToInt32(Console.ReadLine())
' Set value of numberOfBars and couponCount
numberOfBars = dollars
couponCount = numberOfBars
' Begin loop. Loop will determine total bars and remaining coupons
Do While couponCount >= 7
couponCount = couponCount - 7
numberOfBars = numberOfBars + 1
couponCount = couponCount + 1
Loop
' Output values for ending number of chocolate bars and remaining coupons
Console.WriteLine("The total number of chocolate bars you receive is: " & numberOfBars)
Console.WriteLine("The number of coupons you have left is: " & couponCount)
End Sub
Okey I have figure it out just how do i stop the program if someone enters No?
For the 58 compared to 57, I think 57 is actually what you are looking for - 50 bars plus (50/7) = 7.14 round down to 7 = 57
In the case where someone only buys 1 bar, you wouldn't want them to get a free bar, would you?
If you do want 58, then you need to use Double's for your maths and you need to use Math.Ceiling to get the next number up.
For the number of coupons left, I think you need to change:
leftOverCoupons = freeChocolate Mod numberofChocolate
to
leftOverCoupons = freeChocolate Mod 7
to improve the readability of your code a little, you should probably also define a local constant for this "magic number" of 7
Related
My code:
Imports System
Module Program
Sub Main(args As String())
Dim Price As Integer
Dim Qty As Integer
Dim Total As Integer
Console.Write("Enter the product unit price. ")
Price = Console.ReadLine()
Console.Write("Enter the product quantity. ")
Qty = Console.ReadLine()
Total = Price * Qty
If (Qty > 99) And (Qty < 150) Then
Console.WriteLine("You did not qualify for a discount. Your total is: " + Total.ToString)
End If
If (100 < Qty > 149) Then
Console.WriteLine("You qualified for a 10% discount! Your total is: " + (Total * 1.1).ToString)
End If
If (Qty > 149) And (Qty < 200) Then
Console.WriteLine("You qualified for a 15% discount! Your total is: " + (Total * 1.15).ToString)
End If
If (Qty > 199) Then
Console.WriteLine("You qualified for a 20% discount! Your total is: " + (Total * 1.2).ToString)
End If
End Sub
End Module
I can run it to input the values but crash when the math component is executed. Any ideas on how to fix this silly little program?
This isn't a thing:
If (100 < Qty > 149) Then
If needs to look more like this:
If 100 <= Qty AndAlso Qty < 150 Then
The first condition should also just be If Qty < 100 Then
Worse, Qty is a STRING. For cultural/internationalization reasons, converting between strings and numbers is one of the slowest and most error-prone operations you can do within a single computer. It's something to minimize. Therefore you should parse the Qty and Price variables into actual Integer and Decimal variables once right after you read them from the user. Failure to do so makes the code seem amateurish.
Finally, rather than a bunch of If/Else conditions I'd put the data in an ordered map and take the first match:
Imports System
Imports System.Linq
Imports System.ValueTuple ' No longer needed on recent versions of VS
Module Program
Sub Main(args As String())
Console.Write("Enter the product unit price. ")
Dim Price As Decimal = Decimal.Parse(Console.ReadLine())
Console.Write("Enter the product quantity. ")
Dim Qty As Integer = Integer.Parse(Console.ReadLine())
Dim Total As Decimal = Price * Qty
Dim discount As Decimal = GetDiscount(Qty)
Dim discountMessage As String = "You did not qualify for a discount."
If discount > 0.0D Then
discountMessage = $"You qualified for a {discount:P0} discount!"
Total *= (1.0D - discount)
End If
Console.WriteLine($"{discountMessage} Your total is: {Total:C2}")
End Sub
Public Function GetDiscount(Qty As Integer) As Decimal
Dim discountMap() As (Integer, Decimal) = {
(200, 0.20D),
(150, 0.15D),
(100, 0.10D),
(Integer.MinValue, 0.00D)
}
Return discountMap.First(Function(m) m.Item1 <= Qty).Item2
End Function
End Module
If some of this code looks strange to you, it relies on Tuples, a lambda expression, and string interpolation; all three techniques are good tricks to add to your programming repertoire.
i'm pretty new to programming and i got stuck trying to write a program where you type in the name and prices of products and you get back the total, the name+ prices and the most expensive product. Everything works fine except showing the name of the most expensive product.
Here's what i've done
""
Public Class Mrj
Shared Sub main()
Dim i, n As Integer
Console.WriteLine("Enter the number of products")
n = Console.ReadLine()
Dim Products_name(n) As String
Dim Products_price(n), HT, TTC, TVA, max As Decimal
For i = 1 To n
Console.WriteLine("Enter the name of the product " & i)
Products_name(i - 1) = Console.ReadLine()
Console.WriteLine("Enter the price of the product " & i)
Products_price(i - 1) = Console.ReadLine()
HT = HT + Products_price(i - 1)
Next
For i = 1 To n
Console.WriteLine(Products_name(i - 1) & " " & Products_price(i - 1))
Next
TVA = 0.2 * HT
TTC = HT + TVA
Console.WriteLine("Total to pay " & TTC)
max = Products_price(0)
For i = 1 To n - 1
If max > Products_price(i) Then
Else
max = Products_price(i)
End If
Next
Console.WriteLine("The product the most expensive is" & max & Products_name(i))
End Sub
End Class
""
I think the problem is that you are using i to get the name of the most expensive product, but that index i is always i = n since you don't save the index of the maximum value.
You should add a new variable where you store the index everytime you get a new maximum value, and use it in the last line.
Your for loop should be something like this:
Dim max_index As Integer
For i = 1 To n - 1
If max > Products_price(i) Then
Else
max = Products_price(i)
max_index = i
End If
Next
Console.WriteLine("The product the most expensive is" & max & Products_name(max_index))
Try this out and check if it works.
Turn on Option Strict now and forever. Project Properties -> Compile tab. Also for future projects Tools -> Options -> Projects and Solutions -> VB Defaults
You cannot assume that a user will actually enter a number. Test with TryParse.
Arrays in vb.net are declared Products_name(upper bound). In this case that would be Products_name(n-1)
Instead of doing i - 1 for the indexes in the For loop, start our with For i = 0 to n-1
I decided to not use the parallel arrays. Instead I made a tiny class and declared a List(Of Product). I filled the list with the user input setting the Properties of the Product.
I used Linq instead of loops for sums and max. Not necessarily faster but can be accomplished in a single line of code.
I use interpolated strings to display results. When your string is preceded by a $, you can insert variables directly in the text surrounded by braces. The colon following Price indicates a formatting character. Here, I used a C for currency.
Public Class Product
Public Property Name As String
Public Property Price As Decimal
End Class
Sub main()
Dim ProductList As New List(Of Product)
Dim n As Integer
Console.WriteLine("Enter the number of products")
Integer.TryParse(Console.ReadLine, n)
For i = 1 To n
Dim p As New Product
Dim pr As Decimal
Console.WriteLine("Enter the name of the product " & i)
p.Name = Console.ReadLine()
Console.WriteLine("Enter the price of the product " & i)
Decimal.TryParse(Console.ReadLine, pr)
p.Price = pr
ProductList.Add(p)
Next
For Each p In ProductList
Console.WriteLine($"{p.Name} {p.Price:C}")
Next
Dim SubTotal As Decimal = ProductList.Sum(Function(item) item.Price)
Dim Tax As Decimal = 0.2D * SubTotal
Dim Total = SubTotal + Tax
Console.WriteLine($"Total to pay {Total:C}")
Dim Prod = ProductList.OrderByDescending(Function(p) p.Price).FirstOrDefault()
Console.WriteLine($"The product the most expensive is {Prod.Name} at {Prod.Price:C}")
Console.ReadKey()
End Sub
The following is an exercise I am having trouble with.
The button’s Click event procedure should display the number of integers from 14 to 23 in one of the labels and the sum of those integers in the other label. Code the procedure using the For…Next statement. Save the solution and then start and test the application. (The procedure should display the numbers 10 and 185.)
I'm able to display the sum 185 but not understanding how to display the amount of numbers (10) between 14 to 23. Any help is appreciated.
Public Class frmMain
Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
Dim intSum As Integer
For intNum As Integer = 14 To 23
lblShow.Text = lblShow.Text & intNum.ToString & " "
intSum += intNum
lblSum.Text = intSum.ToString
Next
End Sub
End Class
Just declare sum and count local variables and increment them appropriately in the For. You need only update the Label.Text once after the calculations have been done
Dim intSum As Integer
Dim intCount As Integer
For intNum As Integer = 14 To 23
intSum += intNum
intCount += 1
Next
lblSum.Text = intSum.ToString()
lblShow.Text = intCount.ToString()
I understand your homework has the For requirement, but .NET has some functionality built in which can produce a list of numbers, sum them, and count them.
Dim start = 14
Dim finish = 23
Dim numbers = Enumerable.Range(start, finish - start + 1)
lblSum.Text = numbers.Sum().ToString()
lblShow.Text = numbers.Count().ToString()
Both methods produce your required output
display the number of integers from 14 to 23
This can be interpreted in a couple of ways:
Display the count of integers between the two numbers (e.g. 23 - 14)
Display each individual integer between the two numbers (e.g. 14, 15, 16, etc.)
If it is the former, then simply subtract the larger number from the smaller number and display that value in a label:
LabelIntegerCount.Text = (23 - 147).ToString()
If it is the latter, then inside of your For/Next loop append the currently iterated counter to the label:
For intNum As Integer = 14 To 23
LabelIntegerCount.Text &= intNum.ToString() & Environment.NewLine
' ...
Next
Hey im trying to write a small VBA program that calculates the average quiz score the problem is when i enter in the third number it comes back incorrect after doing the first two right.What am i missing?
This is the design view
Option Explicit
Dim total As Double
Dim number As Double
Dim average As Double
Private Sub CommandButton1_Click()
If IsNumeric(TextBox1.Value) = True Then
total = CDbl(average + TextBox1.Value)
number = CDbl(number + 1)
average = CDbl(total / number)
TextBox2.Value = number
TextBox3.Value = average
TextBox1.Value = ""
Else
MsgBox ("please enter a number")
TextBox1.Value = ""
End If
End Sub
Your math is off. If you want to do it, using the previous average, and the new value, then the formula you should use is this:
( [old average] + ( [new value] / [count - 1] ) ) / ( [count] / [count - 1] )
However, as you might see, this is not a very easy-to-read design. I would go for a design, using an array. That way you can keep the math a lot more intuitive, plus you can look back at old score, if you want. The easiest design is probably to keep the array the size of your score count. This requires redim() each time you add a new score. From a performance point-of-view, this is quite bad practice. But in your tiny calculator, thats hardly going to be a problem.
I think you should get number, average, and total value before you do the calculations:
Option Explicit
Dim total As Double
Dim number As Double
Dim average As Double
Private Sub CommandButton1_Click()
number = 0
average = 0
total = 0
If IsNumeric(TextBox1.Value) = True Then
total = CDbl(average + TextBox1.Value)
number = CDbl(number + 1)
'prevent error
If number > 0 then
average = CDbl(total / number)
Else
average = 0
End If
TextBox2.Value = number
TextBox3.Value = average
TextBox1.Value = ""
Else
MsgBox ("please enter a number")
TextBox1.Value = ""
End If
End Sub
Hope this help.
I need to calculate and get a total amount for a unit with multiple calculation conditions. If I enter an amount in a textbox and press enter the second textbox should display a total amount for the below conditions.
(if unit = 450)
0-100 = 3.00 // 101-200 = 5.00 // 201-300 = 7.00 // over 300 = 10.00
if i press enter after entering the unit as 450 second textbox should display total as 2000.00. I am new to vb.net can anyone help me to do this
(for first 100 => 300.00 / second 100 => 500.00 / third 100 => 700.00 / fourth 100 => 500.00 altogether totally 2000.00)
Here is a simple console application to illustrate the process. The idea is to go from the highest range to the lowest and always check if we have to take items from the current range.
Class AmountLimit
Public Property AmountLowerBound As Integer
Public Property Value As Double
Public Sub New(amountLowerBound As Integer, value As Double)
Me.AmountLowerBound = amountLowerBound
Me.Value = value
End Sub
End Class
Sub Main()
'Specify the values for each range. The first number is the lower bound for the range
Dim amountLimits As New List(Of AmountLimit)
amountLimits.Add(New AmountLimit(0, 3))
amountLimits.Add(New AmountLimit(100, 5))
amountLimits.Add(New AmountLimit(200, 7))
amountLimits.Add(New AmountLimit(300, 10))
Console.Write("Enter the total amount: ")
Dim totalAmount = Integer.Parse(Console.ReadLine())
Dim finalValue As Double = 0
For i As Integer = amountLimits.Count - 1 To 0 Step -1
'Check if our total amount is greater than the current lower bound
Dim currentRange = amountLimits(i)
If (totalAmount > currentRange.AmountLowerBound) Then
finalValue += (totalAmount - currentRange.AmountLowerBound) * currentRange.Value
totalAmount = currentRange.AmountLowerBound
End If
Next
Console.WriteLine(finalValue)
End Sub