簡體   English   中英

在C#中使用ArrayList創建計算器類型的Web應用程序

[英]Using ArrayList in C# to create a calculator type web application

我需要使用ArrayList為網頁創建計算器類型的應用程序。 計算器根據幾件不同的事情計算用戶燃燒了多少卡路里。

用戶在其中輸入值的三個文本框。 它們被標記為:活動,體重和持續時間。 在“活動性”框中,用戶輸入一項活動(划獨木舟,釣魚,打高爾夫球,打獵,跑步或散步)。 在重量框中,用戶輸入重量。 持續時間是用戶執行選定活動的時間,以分鍾為單位。 每種活動都會根據輸入的體重燃燒不同量的卡路里。 例如,如果用戶為該活動輸入“划獨木舟”,為重量輸入120,並為該時間輸入60,則所需的輸出將是236卡路里的燃燒量。 但是,如果用戶輸入150作為重量,則結果將是281。

這有點令人困惑,但是基本上燃燒的卡路里取決於人的體重。 這三個范圍是0-130、131-155、156-180和181-205。

誰能幫我嗎? 我知道這可能很難理解,所以請讓我知道是否需要澄清。 我將在下面發布到目前為止的代碼:

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

    ArrayList rangeSmallest = new ArrayList();
    ArrayList rangeSmall = new ArrayList();
    ArrayList rangeBig = new ArrayList();
    ArrayList rangeBiggest = new ArrayList();
    ArrayList activity = new ArrayList();

void Page_Load()
    {

        rangeSmallest.Add(236);
        rangeSmallest.Add(177);
        rangeSmallest.Add(266);
        rangeSmallest.Add(295);
        rangeSmallest.Add(472);
        rangeSmallest.Add(148);

        rangeSmall.Add(281);
        rangeSmall.Add(211);
        rangeSmall.Add(317);
        rangeSmall.Add(352);
        rangeSmall.Add(563);
        rangeSmall.Add(176);

        rangeBig.Add(327);
        rangeBig.Add(245);
        rangeBig.Add(368);
        rangeBig.Add(409);
        rangeBig.Add(654);
        rangeBig.Add(204);

        rangeBiggest.Add(372);
        rangeBiggest.Add(279);
        rangeBiggest.Add(419);
        rangeBiggest.Add(465);
        rangeBiggest.Add(745);
        rangeBiggest.Add(233);

        activity.Add("Canoeing");
        activity.Add("Fishing");
        activity.Add("Golfing");
        activity.Add("Hunting");
        activity.Add("Running");
        activity.Add("Walking");
    }

    void btnSubmit_Click(object sender, EventArgs e)
{



}

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 Activity:   <asp:TextBox ID="txtActivity" runat="server" /><br />
 Weight: <asp:TextBox ID="txtWeight" runat="server" /><br />
 Duration (in minutes): <asp:TextBox ID="txtDuration" runat="server" /><br />
<asp:Button ID="btnSubmit" runat="server" Text="Calories Burned"
    OnClick="btnSubmit_Click" />
<asp:Button ID="btnReset" runat="server" Text="Reset" />
        <asp:Label ID="lblCaloriesBurned" runat="server" />
    </div>
    </form>
</body>
</html>

我將創建一個稱為ExerciseActivity的對象

public class ExerciseActivity
{
    //Hold the name, and the calorie details in here
    String Name {get;set;}
    double lowCalorieUse {get;set;}
    double mediumCalorieUse {get;set;}
    double highCalorieUse {get;set;}

    //Then to calculate it, its pretty easy

    public double CalculateCaloriesBurnt(double mass, double time)
    {

        double calories = 0;

        if (mass < 130)
        {
            calories = lowCalorieUse;
        }
        //the rest is fairly obvious

        //...

        //Multiply by the time
        return calories * time;
    }

現在,在您的主代碼中,您將使用Dictionary來代替...

Dictionary<string, ExerciseActivity>

然后,您可以做類似這樣的好事(注意,您將需要進行檢查)

exerciseDict[txtExerciseName.text].CalculateCaloriesBurnt(mass,time);

(我意識到我有點超出了最初的要求-我不使用ArrayList-但這應該更簡潔。如果您有任何問題,請隨時提問)

根據您的問題我可以推斷出,您想使用該活動的體重類別索引處的數值。

因此,我猜您有6種重量類型,從最重到最輕,然后選擇活動。

您想從length類別中的重量類別中返回索引,(因此,如果用戶選擇了第二個重量類別,則索引為[1],您將在該索引中返回匹配的length類別。

話雖如此,我將刪除您的代碼,並使用有意義的PROPER名稱以及面向對象的方法從頭開始編寫代碼。

如果您想閱讀有關ArrayList的信息,請單擊此鏈接: MSDN官方ArrayList文檔

使用ArrayLists(因為它是特別要求的。這絕不是最佳實踐,也不是使用OOP)。 我讓它盡可能簡單

public double CalculateCalories(string text, double mass, double time)
{
    int index = -1;

    //First we find the index we're interested in
    for(int i=0; i < activity.count; i++)
    {
        string act = activity[i].ToString();

        //If we match the string perfectly, then we know the index
        if (act.Equals(text))
        {
            index = i;
        }

    }

    if (index.Equals(-1))
    {
        //error - throw some sort of exception
    }

    //Now that we know the index, we'll determine which arrayList we look for

    double calorieCount  = 0;

    if (mass < 130)
    {
        //take from the small one
        calorieCount = rangeSmall[index];
    }
    else if (//Follow the same pattern)
    {
        //
    }

    //Then multiply it by time somehow - depending on how your multiplier is set up

    return time*calorieCount;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM