显示 vb 中最贵产品的名称
Showing the name of the most expensive product in vb
我是编程的新手,我在尝试编写一个程序时遇到了困难,在该程序中,您输入产品的名称和价格,然后返回总计、名称+价格和最昂贵的产品。除了显示最昂贵产品的名称外,一切正常。
这是我所做的
""
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
来获取最昂贵产品的名称,但索引 i
始终是 i = n
因为您没有保存最大值的索引。
你应该在每次获得新的最大值时在存储索引的地方添加一个新变量,并在最后一行使用它。
你的 for
循环应该是这样的:
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))
试试这个,看看它是否有效。
现在永远启用 Option Strict。项目属性 -> 编译选项卡。同样对于未来的项目工具 -> 选项 -> 项目和解决方案 -> VB 默认值
您不能假定用户实际上会输入数字。用 TryParse
.
测试
vb.net 中的数组声明为 Products_name(upper bound)
。在这种情况下,这将是 Products_name(n-1)
不要对 For
循环中的索引执行 i - 1
,而是从 For i = 0 to n-1
开始
我决定不使用并行数组。相反,我做了一个很小的 class 并声明了一个 List(Of Product)
。我用用户输入设置 Product
的 Properties
来填充列表。
我使用 Linq 而不是循环求和和最大值。不一定更快,但可以在一行代码中完成。
我使用内插字符串来显示结果。当您的字符串前面有 $
时,您可以直接在用大括号括起来的文本中插入变量。 Price
后面的冒号表示格式化字符。在这里,我使用 C
作为货币。
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
我是编程的新手,我在尝试编写一个程序时遇到了困难,在该程序中,您输入产品的名称和价格,然后返回总计、名称+价格和最昂贵的产品。除了显示最昂贵产品的名称外,一切正常。 这是我所做的
""
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
来获取最昂贵产品的名称,但索引 i
始终是 i = n
因为您没有保存最大值的索引。
你应该在每次获得新的最大值时在存储索引的地方添加一个新变量,并在最后一行使用它。
你的 for
循环应该是这样的:
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))
试试这个,看看它是否有效。
现在永远启用 Option Strict。项目属性 -> 编译选项卡。同样对于未来的项目工具 -> 选项 -> 项目和解决方案 -> VB 默认值
您不能假定用户实际上会输入数字。用 TryParse
.
vb.net 中的数组声明为 Products_name(upper bound)
。在这种情况下,这将是 Products_name(n-1)
不要对 For
循环中的索引执行 i - 1
,而是从 For i = 0 to n-1
我决定不使用并行数组。相反,我做了一个很小的 class 并声明了一个 List(Of Product)
。我用用户输入设置 Product
的 Properties
来填充列表。
我使用 Linq 而不是循环求和和最大值。不一定更快,但可以在一行代码中完成。
我使用内插字符串来显示结果。当您的字符串前面有 $
时,您可以直接在用大括号括起来的文本中插入变量。 Price
后面的冒号表示格式化字符。在这里,我使用 C
作为货币。
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