如何在切换案例中使用值?

时间:2015-05-01 05:16:53

标签: c# switch-statement

我对C#编程很陌生,所以最近我被分配到一个作业来计算" AverageSpeed"然后将它与" maxSpeed"。

相匹配

我遇到了" maxSpeed"因为无论我输入什么价值,我都会得到一个"你在速度限制内#34;我知道我不应该收到这样的消息。我怀疑他们正在使用" maxSpeed"在这种情况下,0不是最大速度。

该计划的目的是要求用户提供驾驶员姓名,车辆类型以及持续时间(以分钟为单位)的行驶距离,以确定驾驶员是否超过了最大允许速度。

请告知。

private void btnSubmit_Click(object sender, EventArgs e)
{
    int minutes = Convert.ToInt32(txtDuration.Text);
    double miles = Convert.ToInt32(txtDistance.Text);
    double hours = minutes / 60;
    double AverageSpeed = GetMPH(hours, miles);

    Object itemSelect = (Object)cboCarType.SelectedItem;
    string carType = itemSelect.ToString();

    int maxSpeed =0;
    switch (carType)
    {
        case "Sedan":
            maxSpeed = 100;
            break;
        case "Wagon":
            maxSpeed = 90;
            MessageBox.Show("Wagon");
            break;
        case "Truck":
            maxSpeed = 50;
            break;
        case "Van":
            maxSpeed = 60;
            break;
        case "Bus":
            maxSpeed = 50;
            break;
        case "Tractor trailer":
            maxSpeed = 30;
            break;
    }

    if (AverageSpeed > maxSpeed)
    {
        MessageBox.Show("You have exceed the maximun speed!");
    }
    else
    {
        MessageBox.Show("You are within the speed limit!");
    }

}

double GetMPH(double miles, double hours) 
{
    double GetMPH = miles / hours;
    return GetMPH;
}

4 个答案:

答案 0 :(得分:1)

逐步执行代码,找出AverageSpeed和maxSpeed的值。我怀疑,根据您的错误描述,AverageSpeed始终为零。

将您的switch更改为一个功能,如下所示:

public int MaxSpeed(string carType) 
{
   switch (carType)
    {
        case "Sedan": return 100;
        case "Wagon": return 90;
        case "Truck": return 50;
        case "Van": return 60;
        case "Bus": return 50
        case "Tractor trailer": return 30;
    }
    return 0;
}

这将允许您单独对该功能进行单元测试并证明其有效。

答案 1 :(得分:1)

可能switch语句不会将maxSpeed更新为所选值。 在switch语句处放置一个断点,以查看carType的值。

答案 2 :(得分:1)

问题在于您的carType字符串未正确设置。 将itemSelect.ToString()更改为itemSelect.Text

private void btnSubmit_Click(object sender, EventArgs e)
{

    int minutes = Convert.ToInt32(txtDuration.Text);
    double miles = Convert.ToInt32(txtDistance.Text);
    double hours = minutes / 60;
    double AverageSpeed = GetMPH(miles, hours);

    Object itemSelect = (Object)cboCarType.SelectedItem;
    string carType = itemSelect.Text;

    int maxSpeed =0;
        switch (carType)
        {
            case "Sedan":
                maxSpeed = 100;
                break;
            case "Wagon":
                maxSpeed = 90;
                MessageBox.Show("Wagon");
                break;
            case "Truck":
                maxSpeed = 50;
                break;
            case "Van":
                maxSpeed = 60;
                break;
            case "Bus":
                maxSpeed = 50;
                break;
            case "Tractor trailer":
                maxSpeed = 30;
                break;
        }
         if (AverageSpeed > maxSpeed)
        MessageBox.Show("You have exceed the maximun speed!");
    else
        MessageBox.Show("You are within the speed limit!");

}

double GetMPH(double miles, double hours) 
{
    double GetMPH = miles / hours;
    return GetMPH;
}

更新: 您还可以使用以下代码获取carType。

//Object itemSelect = (Object)cboCarType.SelectedItem; //Remove this line
string carType = cboCarType.SelectedItem.Text;

将GetMPH函数调用更改为GetMPH(英里,小时)

答案 3 :(得分:0)

您已完成以下代码:

int minutes = Convert.ToInt32(txtDuration.Text);
double miles = Convert.ToInt32(txtDistance.Text);
double hours = minutes / 60;
double AverageSpeed = GetMPH(hours, miles);

将以下代码替换为:

double minutes = Convert.ToDouble(txtDuration.Text);
double miles = Convert.ToDouble(txtDistance.Text);
double hours = minutes / 60;
double AverageSpeed = GetMPH(miles, hours);

并确保在string carType中获得正确的选定值。

希望这会对你有所帮助。