0

Web ページ用の電卓タイプのアプリケーションを作成するには、ArrayList を使用する必要があります。計算機は、いくつかの異なる事柄に基づいて、ユーザーが消費したカロリー数を計算します。

ユーザーが値を入力する 3 つのテキスト ボックスがあります。それらには、アクティビティ、体重、および期間というラベルが付けられています。[アクティビティ] ボックスに、ユーザーはアクティビティ (カヌー、釣り、ゴルフ、狩猟、ランニング、ウォーキング) を入力します。重量ボックスに、ユーザーは重量を入力します。期間は、ユーザーが選択したアクティビティを実行した時間 (分単位) です。各アクティビティは、入力された体重に応じて異なる量のカロリーを消費します。たとえば、ユーザーがアクティビティにカヌー、体重に 120、時間に 60 と入力した場合、必要な出力は 236 カロリーの消費になります。ただし、ユーザーが重みに 150 を入力すると、結果は 281 になります。

少しややこしいですが、基本的には人の体重によって消費カロリーが異なります。3 つの範囲は、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>
4

3 に答える 3

0

私は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<string, ExerciseActivity>

次に、このような素晴らしいことを行うことができます(チェックを入れる必要があることに注意してください)

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

(元の要件から少し外れていることに気付きました - 私は ArrayList を使用していません - しかし、これはもっときれいなはずです。質問があればお気軽に聞いてください)

于 2013-11-09T22:56:42.733 に答える
0

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;

}
于 2013-11-10T07:53:03.030 に答える