简体   繁体   English

在C#中使用ArrayList创建计算器类型的Web应用程序

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

I need to use ArrayList to make a calculator type application for a web page. 我需要使用ArrayList为网页创建计算器类型的应用程序。 The calculator calculates how many calories the user has burned based on a couple different things. 计算器根据几件不同的事情计算用户燃烧了多少卡路里。

There are three textboxes that the user enters value into. 用户在其中输入值的三个文本框。 They are labeled: Activity, Weight, and Duration. 它们被标记为:活动,体重和持续时间。 In the the Acitivity box, the user enters an activity (either canoeing, fishing, golfing, hunting, running, or walking). 在“活动性”框中,用户输入一项活动(划独木舟,钓鱼,打高尔夫球,打猎,跑步或散步)。 In the weight box, the user enters a weight. 在重量框中,用户输入重量。 The duration is how long the user performed the selected activity in minutes. 持续时间是用户执行选定活动的时间,以分钟为单位。 Each activity burns a different amount of calories depending on the weight entered. 每种活动都会根据输入的体重燃烧不同量的卡路里。 For example, if the user enters Canoeing for the activity, and 120 for the weight, and 60 for the time, the desired output will be 236 calories burned. 例如,如果用户为该活动输入“划独木舟”,为重量输入120,并为该时间输入60,则所需的输出将是236卡路里的燃烧量。 However, if the user enters 150 for the weight, the outcome will be 281. 但是,如果用户输入150作为重量,则结果将是281。

It's a little bit confusing, but basically the calories burned vary depending on the weight of the person. 这有点令人困惑,但是基本上燃烧的卡路里取决于人的体重。 The three ranges are 0-130, 131-155, 156-180, and 181-205. 这三个范围是0-130、131-155、156-180和181-205。

Can anyone help me out? 谁能帮我吗? I know it might be hard to understand so let me know if anything needs clarification. 我知道这可能很难理解,所以请让我知道是否需要澄清。 I'll post the code I have so far below: 我将在下面发布到目前为止的代码:

<%@ 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>

I'd create an object which I'll call ExerciseActivity 我将创建一个称为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;
    }

Now in your main code, you use a Dictionary Instead... 现在,在您的主代码中,您将使用Dictionary来代替...

Dictionary<string, ExerciseActivity>

Then you can do something nice like this (note, you WILL need to put in checks) 然后,您可以做类似这样的好事(注意,您将需要进行检查)

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

(I realize I went a bit out of the original requirements - I don't use an ArrayList - but this should be much cleaner. If you have any questions feel free to ask) (我意识到我有点超出了最初的要求-我不使用ArrayList-但这应该更简洁。如果您有任何问题,请随时提问)

From what I can infer from your question, you want to use the numeric value at the index of the weight category of the activity. 根据您的问题我可以推断出,您想使用该活动的体重类别索引处的数值。

So I'll guess you have 6 weight types, going from the heaviest to the lightest, and then you select the activity. 因此,我猜您有6种重量类型,从最重到最轻,然后选择活动。

You want to return the index from the weight category in the length one, (so if the user selected the 2nd weight category, the index will be [1], you'll return the matching length category in that index. 您想从length类别中的重量类别中返回索引,(因此,如果用户选择了第二个重量类别,则索引为[1],您将在该索引中返回匹配的length类别。

Having said that, I would delete your code, and write it from scratch, using PROPER names that make sense, and using Object oriented methods. 话虽如此,我将删除您的代码,并使用有意义的PROPER名称以及面向对象的方法从头开始编写代码。

If you want to read about ArrayLists, please, click this link: MSDN official ArrayList documentation . 如果您想阅读有关ArrayList的信息,请单击此链接: MSDN官方ArrayList文档

Using ArrayLists (Since it was specifically asked for. It is in no way the best practice, nor does it make use of OOP). 使用ArrayLists(因为它是特别要求的。这绝不是最佳实践,也不是使用OOP)。 I'm keeping it as simple as possible 我让它尽可能简单

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