1
protected void calculateRateButton_Click(object sender, EventArgs e)
{
    //declare variables and retrieve user inputs
    int nbPeopleInt;
    string roomTypeString;
    int nbNightsInt;

    nbNightsInt = int.Parse(nbNightsTextBox.Text);
    nbPeopleInt = int.Parse(nbPeopleDropDownList.Text);
    roomTypeString = roomTypeDropDownList.Text;

    int totalFareInt = calculateRoomBooking(roomTypeString, nbPeopleInt, nbNightsInt);
}

private int calculateRoomBooking(string RoomType, int nbNights, int nbPeople)
{

    int totalFareInt = 0;
    int dailyRateInt = 0;

    // set daily rate based on selected room type
    switch (RoomType)
    {
        case "Standard":
            dailyRateInt = 100;
            break;
        case "Superior":
            dailyRateInt = 150;
            break;
        case "Luxury":
            dailyRateInt = 175;
            break;
    }

    // calculation of booking rate
    totalFareInt = dailyRateInt * nbNightsInt + 10 * (nbPeopleInt - 1) * nbNightsInt;

    // display booking rate
    totalRateTextBox.Text = totalFareInt.ToString();
}

protected void roomDetailsButton_Click(object sender, ImageClickEventArgs e)
{
    // go to room details page
    Response.Redirect("RoomDetails.aspx");
}

但是,它总是说 dailyRateInt, nbNightsInt, nbPeopleInt 没有用这一行定义:

totalFareInt = dailyRateInt * nbNightsInt + 10 * (nbPeopleInt - 1) * nbNightsInt;

但是我确实在上面定义了它们,你们可以看到

请帮忙

4

3 回答 3

2

该变量nbPeopleInt是局部变量calculateRateButton_Click,它在方法范围之外使用。因此,您遇到了问题。

您可能会在方法之外定义nbPeopleInt,roomTypeStringnbNightsIntcalculateRateButton_Click

您可能想要这样做:-

private int calculateRoomBooking(string RoomType, int nbNights, int nbPeople)
{

    int totalFareInt = 0;
    int dailyRateInt = 0;

    // set daily rate based on selected room type
    switch (RoomType)
    {
        case "Standard":
            dailyRateInt = 100;
            break;
        case "Superior":
            dailyRateInt = 150;
            break;
        case "Luxury":
            dailyRateInt = 175;
            break;
    }

   // calculation of booking rate
   totalFareInt = dailyRateInt * nbNights + 10 * (nbPeople - 1) * nbNights;
    // USING nbNights AND nbPeople instead of nbNightsInt and nbPeopleInt

   // display booking rate
   totalRateTextBox.Text = totalFareInt.ToString();
   return 1;     //Change 1 to the value which you want to return from the function.
}
于 2013-09-15T16:16:12.193 回答
1

您错误地尝试使用函数参数时nbNightsIntnbPeopleIntnbNightsnbPeople

做:

private int calculateRoomBooking(string RoomType, int nbNights, int nbPeople)
{

    int totalFareInt = 0;
    int dailyRateInt = 0;

    // set daily rate based on selected room type
    switch (RoomType)
    {
        case "Standard":
            dailyRateInt = 100;
            break;
        case "Superior":
            dailyRateInt = 150;
            break;
        case "Luxury":
            dailyRateInt = 175;
            break;
    }

   // calculation of booking rate
   totalFareInt = dailyRateInt * nbNights + 10 * (nbPeople - 1) * nbNights;
    // USING nbNights AND nbPeople instead of nbNightsInt and nbPeopleInt

   // display booking rate
   totalRateTextBox.Text = totalFareInt.ToString();
}
于 2013-09-15T16:17:12.767 回答
0

您正在尝试使用变量“nbNightsInt”和“nbPeopleInt”而不是“nbNights”和“nbPeople”,而这两个变量在该范围内不存在。只需更改该行上的变量名称:

totalFareInt = dailyRateInt * nbNights + 10 * (nbPeople - 1) * nbNights;
于 2013-09-15T16:20:30.740 回答