好的,所以我一直在写一个厨房测量课,现在它只有体积或液体测量,我称之为:
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;
}
}
}
答案 0 :(得分: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杯”