计算平均值Visual Studio 2012,vb [英] Calculate average Visual studio 2012, vb

查看:144
本文介绍了计算平均值Visual Studio 2012,vb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮帮我如何计算combobox.Text的平均值...



 ComboBox1.Items.Add(  4
ComboBox1.Items.Add( 5
ComboBox1.Items.Add( 6
ComboBox1.Items.Add( 7
ComboBox1.Items.Add( 8
ComboBox1.Items.Add( 9
ComboBox1.Items.Add( 10
ComboBox1.SelectedItem = 0

ComboBox2.Items.Add( 4
ComboBox2.Items.Add( 5
ComboBox2.Items.Add ( 6
ComboBox2.Items.Add( 7
ComboBox2.Items.Add( 8
ComboBox2.Items.Add( 9
ComboBox2.Items.Add( 10
ComboBox2.SelectedItem = 0

ComboBox3.Items.Add( 4
ComboBox3.Items.Add( 5
ComboBox3.Items.Add( 6
ComboBox3.Items.Add( 7
ComboBox3.Items.Add( 8
ComboBox3。 Items.Add( 9
ComboBox3.Items.Add( 10
ComboBox3.SelectedItem = 0







字符串的平均值

average = combobox1.Text + combobox2.Text + combobox3.Text)/ 3

MsgBox(平均值)

解决方案

而不仅仅是将它们加在一起希望,明确转换:

  Dim 平均作为 < span class =code-keyword>单 
average =(整数 .Parse(combobox1.Text)+ 整数 .Parse(combobox2.Text)+ 整数 .Parse( combobox3.Text))\ 3
MsgBox(average.ToString())



或者,不要添加字符串 - 添加实际值:

 ComboBox1.Items.Add( 4 
ComboBox1.Items.Add( 5
ComboBox1.Items.Add( 6
ComboBox1.Items.Add( 7

然后将SelectedValue属性添加到一起而不是文字属性



帮我提供几个组合框的例子



你必须开始为自己思考。

我不能为你做全部工作!这不是一件困难的事情 - 还有更多的复杂性,所以现在是时候开始思考问题并为自己解决问题了。

但是,我会给你的这一点。

 Dim valuesTotal  As  Integer =  0  
Dim valuesCount As Integer = 0
如果 comboBox1.SelectedIndex> = 0 那么
valuesCount + = 1
valuesTotal + = Integer.Parse(DirectCast(comboBox1.SelectedItem,String))
结束 如果
如果 comboBox2.SelectedIndex> = 0 然后
valuesCount + = 1
valuesTotal + = Integer.Parse(DirectCast(comboBox2.SelectedItem,String))
结束 如果
如果 valuesCount> 0 然后
Dim average As Integer = valuesTotal \ valuesCount
结束 如果





这么难吗?如果使用数值而不是字符串,则更容易:

 Dim valuesTotal  As  Integer =  0  
Dim valuesCount As 整数= 0
如果 comboBox1.SelectedIndex> = 0 然后
valuesCount + = 1
valuesTotal + = CInt(comboBox1.SelectedItem)
结束 如果
如果 comboBox2.SelectedIndex> ; = 0 然后
valuesCount + = 1
valuesTotal + = CInt(comboBox2.SelectedItem)
结束 如果
如果 valuesCount> 0 然后
Dim average As Integer = valuesTotal \ valuesCount
结束 如果







请帮帮我..



我有3个组合框,每个都有咖啡吧项目,如:Water,Capuchino等。

现在我如何在账单中显示它们的价格是多少?

水是8


,如何计算用户选择了组合框中的项目并想知道它的费用是多少?

回复

OriginalGriff - 1小时前

我该怎么做?

我会创建一个名为Product或类似的新类,它有一个文本字符串和一个我会覆盖ToString方法,根据我想要显示的内容生成名称,名称和价格。

然后我会设置将类实例放入组合框中(它调用ToString方法显示一个字符串)并在我想添加值时直接使用它们。

回复

圣德拉 - 55分钟前

你能用一个例子来帮助我...

回复

圣德拉 - 30分钟前

请再帮我一次

回复

OriginalGriff - 13分钟前

你知道如何创建课程,是吗?

回复

圣德拉 - 4分钟前

不,我没有真正创造一个......但请帮助我




A类:

<前lang =vb> 公众 产品
私有 _ProductName As 字符串
私有 _Cost As Double

属性 ProductName 作为 字符串
获取
返回 _ProductName
结束 获取
设置(值 As 字符串
_ProductName = value
结束 设置
结束 属性
属性成本作为 Double
获取
返回 _Cost
结束 获取
设置( value As Double
_Cost = value
结束 设置
结束 属性
公共 覆盖 函数 ToString()作为 字符串
返回 _ProductName& @


& _Cost
结束 功能
结束



添加它的最简单方法是右键单击您的项目并选择添加 ......类......并在按OK之前将其命名为Product - 这将为您创建一个空白模板文件。然后你所要做的就是创建实例,将它们加载到组合框中并获取值 - 你应该知道如何从之前的东西中完成所有这些。


Help me how can I Calculate the average of combobox.Text...

ComboBox1.Items.Add("4")
        ComboBox1.Items.Add("5")
        ComboBox1.Items.Add("6")
        ComboBox1.Items.Add("7")
        ComboBox1.Items.Add("8")
        ComboBox1.Items.Add("9")
        ComboBox1.Items.Add("10")
        ComboBox1.SelectedItem = 0

        ComboBox2.Items.Add("4")
        ComboBox2.Items.Add("5")
        ComboBox2.Items.Add("6")
        ComboBox2.Items.Add("7")
        ComboBox2.Items.Add("8")
        ComboBox2.Items.Add("9")
        ComboBox2.Items.Add("10")
        ComboBox2.SelectedItem = 0

        ComboBox3.Items.Add("4")
        ComboBox3.Items.Add("5")
        ComboBox3.Items.Add("6")
        ComboBox3.Items.Add("7")
        ComboBox3.Items.Add("8")
        ComboBox3.Items.Add("9")
        ComboBox3.Items.Add("10")
        ComboBox3.SelectedItem = 0




Dim average as string
average = combobox1.Text + combobox2.Text + combobox3.Text ) / 3
MsgBox(average)

解决方案

Instead of just adding them together an hoping, do explicit conversions:

Dim average As Single
average = (Integer.Parse(combobox1.Text) + Integer.Parse(combobox2.Text) + Integer.Parse(combobox3.Text)) \ 3
MsgBox(average.ToString())


Alternatively, don''t add strings - add the actual values:

ComboBox1.Items.Add(4)
ComboBox1.Items.Add(5)
ComboBox1.Items.Add(6)
ComboBox1.Items.Add(7)

And then add the SelectedValue properties together instead of the Text property

"help me with an examples of a few combobox pls"

You have got to start thinking for yourself.
I can''t do your whole job for you! This isn''t difficult stuff - there is a lot more complexity to come, so it is time you start to think about things and work them out for yourself.
But, I''ll give you this bit.

Dim valuesTotal As Integer = 0
Dim valuesCount As Integer = 0
If comboBox1.SelectedIndex >= 0 Then
    valuesCount += 1
    valuesTotal += Integer.Parse(DirectCast(comboBox1.SelectedItem, String))
End If
If comboBox2.SelectedIndex >= 0 Then
    valuesCount += 1
    valuesTotal += Integer.Parse(DirectCast(comboBox2.SelectedItem, String))
End If
If valuesCount > 0 Then
    Dim average As Integer = valuesTotal \ valuesCount
End If



Was that so difficult? It''s even easier if you use numeric values instead of strings:

Dim valuesTotal As Integer = 0
Dim valuesCount As Integer = 0
If comboBox1.SelectedIndex >= 0 Then
    valuesCount += 1
    valuesTotal += CInt(comboBox1.SelectedItem)
End If
If comboBox2.SelectedIndex >= 0 Then
    valuesCount += 1
    valuesTotal += CInt(comboBox2.SelectedItem)
End If
If valuesCount > 0 Then
    Dim average As Integer = valuesTotal \ valuesCount
End If




"Help me on this please..

I have 3 comboboxes, each with coffe bar items, like: Water, Capuchino, Etc.
Now How can I Display in bill how much they cost?
water is 8


, how can I count that the users has selected the items in comboboxes and want to know how much it cost?
Reply
OriginalGriff - 1 hr ago
How would I do it?
I''d create a new class, called "Product" or similar, which had a text string and a price. I''d override the ToString method to generate either the name, or the name and the price depending on what I wanted to display.
I''d then set the class instance into the comboboxes (which calls the ToString method to display a string) and just use them directly when I wanted to add value yup.
Reply
San Dra - 55 mins ago
Can You help me with an example plss...
Reply
San Dra - 30 mins ago
Please Help me one more time please
Reply
OriginalGriff - 13 mins ago
You know how to create a class, yes?
Reply
San Dra - 4 mins ago
No i haven''t really create one...But help me pls"


A class:

Public Class Product
    Private _ProductName As String
    Private _Cost As Double

    Property ProductName As String
        Get
            Return _ProductName
        End Get
        Set(value As String)
            _ProductName = value
        End Set
    End Property
    Property Cost As Double
        Get
            Return _Cost
        End Get
        Set(value As Double)
            _Cost = value
        End Set
    End Property
    Public Overrides Function ToString() As String
        Return _ProductName & " @


" & _Cost End Function End Class


The easiest way to add it is to right click your project and select "add...class..." and name it "Product" before pressing OK - that will create a blank template file for you. Then all you have to do is create the instances, load them into your comboboxes and get the values back - and you should know how to do all that already from your previous stuff.


这篇关于计算平均值Visual Studio 2012,vb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆