[英]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.