3

好的,所以我一直在写一个厨房测量类,现在它只有体积或液体测量值,我这样称呼它:

ktchnmsrmnts.VolumeMeasurements(number), current_measurement, to_measurement);

从那里大多数人很容易找出内部代码,只是一些简单的数学运算。现在我的问题是我需要添加一个选项,上面写着‘嘿,他有 17.50 杯,我们让它 1 加仑,和 1 1/2 杯。

我正在考虑向“to_measurement”添加一个新案例,以检查数字是否大于最高测量值,例如 19 杯大于加仑,然后将其设为 1 加仑并将剩余部分发送到线上,但是我从不在厨房里用品脱,所以也许可以添加另一个转换它的选项。我不知道我被卡住了,是的,我知道代码是基本的,但符合我的需要,无论如何..

以防万一你需要我的完整代码,给你:

class KitchenMeasurements
{
    public double VolumeMeasurements(double number, string current_measurement, string to_measurement)
    {
        double mL = 0;
        double tsp= 0;
        double tbsp = 0;
        double fluid_ounces = 0;
        double cup = 0;
        double pint = 0;
        double quart = 0;
        double gallon = 0;
        double ounce = 0;
        double gram = 0;
        double pound = 0;
        double liter = 0;

        switch (current_measurement)
        {
            case "mL":
                mL = number;
                tsp = mL * 0.202884;
                tbsp = mL * 0.067628;
                fluid_ounces = mL * 0.033814;
                cup = mL * 0.00422675;
                pint = mL * 0.00211338;
                quart = mL * 0.00105669;
                gallon = mL * 0.000264172;
                liter = mL * 0.001;
                break;

            case "tsb":
                tsp = number;
                mL = tsp * 4.92892;
                tbsp = tsp * 0.333333;
                fluid_ounces = tsp * 0.166667;
                cup = tsp * 0.0208333;
                pint = tsp * 0.0104167;
                quart = tsp * 0.00520833;
                gallon = tsp * 0.00130208;
                liter = tsp * 0.00492892;
                break;

            case "tbsp":
                tbsp = number;
                mL = tbsp * 14.7868;
                tsp = tbsp * 3;
                fluid_ounces = tbsp * 0.5;
                cup = tbsp * 0.0625;
                pint = tbsp * 0.03125;
                quart = tbsp * 0.015625;
                gallon = tbsp * 0.00390625;
                liter = tbsp * 0.0147868;
                break;

            case "fluid ounce":
                fluid_ounces = number;
                mL = fluid_ounces * 29.5735;
                tsp = fluid_ounces * 6;
                tbsp = fluid_ounces * 2;
                cup = fluid_ounces * 0.125;
                pint = fluid_ounces * 0.0625;
                quart = fluid_ounces * 0.03125;
                gallon = fluid_ounces * 0.0078125;
                liter = fluid_ounces * 0.0295735;
                break;

            case "cup":
                cup = number;
                mL = cup * 236.588;
                tsp = cup * 48;
                tbsp = cup * 16;
                fluid_ounces = cup * 8;
                pint = cup * 0.5;
                quart = cup * 0.25;
                gallon = cup * 0.0625;
                liter = cup * 0.236588;
                break;

            case "pint":
                pint = number;
                mL = pint * 473.176;
                tsp = pint * 96;
                tbsp = pint * 32;
                fluid_ounces = pint * 16;
                cup = pint * 2;
                quart = pint * 0.5;
                gallon = pint * 0.125;
                liter = pint * 0.473176;
                break;

            case "quart":
                quart = number;
                mL = quart * 946.353;
                tsp = quart * 192;
                tbsp = quart * 64;
                fluid_ounces = quart * 32;
                cup = quart * 4;
                pint = quart * 2;
                gallon = quart * 0.25;
                liter = quart * 0.946353;
                break;

            case "gallon":
                gallon = number;
                mL = gallon * 3785.41;
                tsp = gallon * 768;
                tbsp = gallon * 256;
                fluid_ounces = gallon * 128;
                cup = gallon * 16;
                pint = gallon * 8;
                quart = gallon * 4;
                liter = gallon * 3.78541;
                break;

            case "liter":
                liter = number;
                mL = liter * 1000;
                tsp = liter * 202.884;
                tbsp = liter * 67.628;
                fluid_ounces = liter * 33.814;
                cup = liter * 4.22675;
                pint = liter * 2.11338;
                quart = liter * 1.05669;
                gallon = liter * 0.264172;
                break;
        }

        switch (to_measurement)
        {
            case "mL":
                return mL;
            case "tsb":
                return tsp;
            case "tbsp":
                return tbsp;
            case "fluid ounce":
                return fluid_ounces;
            case "cup":
                return cup;
            case "pint":
                return pint;
            case "quart":
                return quart;
            case "gallon":
                return gallon;
            case "ounce":
                return ounce;
            case "gram":
                return gram;
            case "pound":
                return pound;
            case "liter":
                return liter;
        }

    }
}
4

1 回答 1

1

很难确切地说出您要的是什么,但我确实看到了:

现在我的问题是我需要添加一个选项,上面写着‘嘿,他有 17.50 杯,我们让它 1 加仑,和 1 1/2 杯。

您需要进行一组测量并循环遍历它们以重新计算余数。我强烈建议您将方法代码提取到类变量和辅助方法中。

下面是一些可以帮助您的示例代码。您需要填写空白。

public string ConvertToDescription(double amount, string originalMeasurement, params string[] toMeasurements)
{
    StringBuilder sb = new StringBuilder();
    double valueToConvert = amount;
    string priorMeasurement = originalMeasurement;
    double displayAmount;
    for (int i = 0; i < toMeasurements.Count; i++)
    {
         if (i > 0)
             sb.Append(" ");

          double convertedAmount = VolumeMeasurements(valueToConvert, priorMeasurement, toMeasurement[i]);

          // Check if last item so we don't trim wanted decimals.
          if (i < toMeasurements.Count - 1)
              displayAmount = Math.Floor(convertedAmount)
          else
              displayAmount = convertedAmount;

          valueToConvert = convertedAmount - displayAmount;
          priorMeasurement = toMeasurements[i];

          // You will need to add logic here to display fractions if needed.
          sb.AppendFormat("{0} {1}", displayAmount, toMeasurements[i]);

          if (fixedAmount == 0)
              break;
    }

    return sb.ToString();
}

请记住,这些都没有经过测试,我真的希望这是您所要求的。这应该让你走上正确的轨道。您将需要找到一种方法来将最后一次转换的小数转换为分数并实现它。这至少会让你“1加仑1.5杯”

于 2013-05-27T07:26:09.093 回答