匿名使用者
匿名使用者 發問時間: 電腦與網際網路程式設計 · 7 個月前

急急急!!!這題VB要怎麼寫??

一杯咖啡成本2元,

訂價10元可賣出600杯,

訂價20元可賣出500杯,

訂價30元可賣出400杯,

每漲10元,少賣100杯,

每漲1元,少賣10杯,

請問要訂價多少元,能有最大獲利。

假設訂價 x元

(x-2)*(700-10x)最極大值

Attachment image

2 個解答

評分
  • John
    Lv 7
    7 個月前
    最佳解答

    Public Class Form1

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click

    Dim iPrice As Integer = CInt(txtPrice.Text)

    Dim iProfit As Integer = Me.CalculateProfit(iPrice)

    Me.txtProfit.Text = iProfit.ToString()

    Me.txtCup.Text = Me.CalculateCup(iProfit, iPrice).ToString()

    End Sub

    Private Function CalculateProfit(ByVal x As Integer) As Integer

    CalculateProfit = (x - 2) * (700 - 10 * x)

    End Function

    Private Function CalculateCup(ByVal profit As Integer, price As Integer) As Integer

    CalculateCup = profit / (price - 2)

    End Function

    Private Sub btnAnalyse_Click(sender As Object, e As EventArgs) Handles btnAnalyse.Click

    Dim iMaxProfit As Integer = 0

    For iPrice As Integer = 10 To 40

    Dim iProfit As Integer = Me.CalculateProfit(iPrice)

    If iMaxProfit < iProfit Then

    iMaxProfit = iProfit

    txtPrice.Text = CStr(iPrice)

    txtProfit.Text = CStr(iMaxProfit)

    txtCup.Text = Me.CalculateCup(iPrice, iMaxProfit).ToString()

    End If

    Next

    End Sub

    End Class

  • 7 個月前

    公式你都知道了用程式寫出來就好了 不然你直接把每種可能都列出來計算後在程式上顯示阿 做成按鈕也行 方式很多種

還有問題?馬上發問,尋求解答。