visual-studio - 在与变量相关的循环中减去
问题描述
我正在尝试创建一个循环,不断从每个项目的初始金额中减去,直到金额低于项目的价格。它还应该显示在金额低于商品价格之前购买的最后一件商品。汽车售价 310,000 美元,戒指售价 12,500 美元,钻石售价 150,000 美元,巧克力售价 51 美元。这些项目位于我计算机上的一个文件中,以下是它的外观示例。
样本输入:
350000
Car
Ring
Diamond
Car
Chocolate
Diamond
样本输出:
Ring
$27, 500 left
出于某种原因,当我减去时,我得到了错误的值,但我不知道为什么。我已经宣布了每件商品的价格并多次检查以确保它们是正确的,并且我已经检查了我的代码,但我仍然不知道为什么我得到错误的输出。
Private Sub btnS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnS.Click
Dim inFile As StreamReader = New StreamReader("serena.txt")
'Declare the varibles
Dim variableName As String
' the current items from the file
Dim amount As Integer
Dim price As Integer
amount = Val(inFile.ReadLine())
Do
'read in the words
variableName = inFile.ReadLine()
'determine each item's price
If variableName = "car" Then price = 310000
If variableName = "ring" Then price = 12500
If variableName = "diamond" Then price = 150000
If amount >= price Then amount = amount - price
Loop Until amount < price
'output the results
Me.lblOutput.Text = variableName & _
vbNewLine & "Serena has " & Format(amount, "currency") & " left"
End Sub
解决方案
仔细看看我在下面代码中的评论:
Private Sub btnS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnS.Click
Dim fileName As String = Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "serena.txt")
Dim inFile As New StreamReader(fileName)
Dim itemToPurchase As String
Dim lastThingBought As String = "{ Nothing Bought }" ' what if they can't afford anything?
Dim balance As Integer
Dim price As Integer
Dim somethingBought As Boolean
balance = Val(inFile.ReadLine())
Do
somethingBought = False ' we don't know yet if we can purchase the next item
'read in the words
itemToPurchase = inFile.ReadLine().ToLower().Trim() ' make sure it has no spaces and is lowercase to match below
'determine each item's price
Select Case itemToPurchase
Case "car"
price = 310000
Case "ring"
price = 12500
Case "diamond"
price = 150000
Case "chocolate" ' FYI: you were missing chocolate in your code
price = 51
Case Else ' it could happen!
MessageBox.Show(itemToPurchase, "Unknown Item!")
lblOutput.Text = "Error in file"
Exit Sub
End Select
If balance >= price Then
somethingBought = True
balance = balance - price
' we need to store the last thing purchased,
' because "itemToPurchase" gets replaced with the thing you couldn't afford
lastThingBought = itemToPurchase
End If
' Need to check for the end of file being reached...
' ...they may purchase everything in the file and still have money left!
Loop While Not inFile.EndOfStream AndAlso somethingBought
'output the results
Me.lblOutput.Text = lastThingBought &
vbNewLine & "Serena has " & Format(balance, "currency") & " left"
End Sub
推荐阅读
- angular - 带有@angular/pwa 包的 Angular 服务工作者在离线时显示“HTTP 错误 504”
- ios - 由于可用内存不足,制作 iOS Crashlytics 报告的奇怪胡说八道不正确?
- java - spring batch 2.2.7 应用程序抛出“无效的列名”。非常简单的代码炸弹
- bash - a2ensite 不创建日志文件
- laravel - 如何在 laravel 中为不同的用户创建不同的工作区?
- javascript - 如何通过比较值从嵌套对象数组中获取信息以创建另一个对象?
- typo3 - Typo3:如何创建和显示自定义内容元素(例如:电子邮件 ID、电话号码等)
- excel - vba excel,公式,停止excel链接表格中的forumla
- android - 如何将 RecyclerView 项目滚动到边界之外
- python - 使用python从大写/小写的doc文件中获取特定的单词