首先:您在第 141 行有错误。删除该done
单词。我建议这只是复制粘贴错误。其次:在这里你说你的代码是:
else if (type == "cups")
{
Console.WriteLine("Enter a number of liters to be converted into cups.");
amount = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(cupsTolitConverter.Convert(amount) + " cups to said number of liters.");
}
但是在您提供链接的解决方案中,有:
else if(type == "cups")
{
Console.WriteLine("Enter a number of liters to be converted into cups.");
amount = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(cupsTolitConverter.Convert(amount) + " centimeters to said number of inches.");
}
我认为这只是疏忽。您的代码工作正常,只需替换
Console.WriteLine(cupsTolitConverter.Convert(amount) + " centimeters to said number of inches.");
和
Console.WriteLine(cupsTolitConverter.Convert(amount) + " cups to said number of liters.");
在您的解决方案中。
谈论简化您的代码,并删除重复。当然有办法做到这一点。该过程称为代码重构。例如,您在项目中写道:
...
else if(type == "grams")
{
amount = gramsToOunces(ozToGramsConverter, ref done);
}
...
将您的评估amount
替换为gramToOunces() 方法。这在某种程度上也是代码重构。您应该获取代码的常见部分并查看它们依赖的内容。例如:
private static double MilesToFeet(UnitConverter milesToFeetConverter, ref int done)
{
double amount;
Console.WriteLine("Enter a number of miles to be converted to feet.");
amount = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(milesToFeetConverter.Convert(amount) + " feet to said number of miles.");
return amount;
}
您的每个转换方法都有这样的结构。在每种方法中,您都这样做。您打印相同的指令消息...您声明相同的数量变量并将其值设置为控制台输入...然后您编写几乎相同的信息消息。所有这一切,而不是您拥有的 N 种方法,您可以放入 ONE 方法中。首先你应该扩展你的转换器类。添加另外两个字符串字段字段。FromUnit
和ToUnit
。为什么???因为您将转换器实例作为参数提供给您的方法。您可以使用此实例来存储有关此转换器的更多信息,并在之后创建信息消息。例如:
public class UnitConverter
{
double ratio;
public string From { get; set; }
public string To { get; set; }
public UnitConverter(double unitratio) { ratio = unitratio; }
public double Convert(double unit) { return unit * ratio; }
}
然后,当您创建转换器实例时,为这些属性设置适当的值。
UnitConverter milesToFeetConverter = new UnitConverter(5280) { From = "miles" , To = "feet"};
现在让我们编写一个方法,它将代表您已经创建的所有方法(gramsToOunces、kmsToMiles 等);
private static void MakeConversion(UnitConverter customConverter)
{
Console.WriteLine("Enter a number of {0} to be converted to {1}.",customConverter.From, customConverter.To);
double amount = Convert.ToDouble(Console.ReadLine());
string message = string.Format("{0} {1} to said number of {2}", amount, customConverter.To, customConverter.From);
Console.WriteLine(message);
}
现在您可以删除所有方法,并且在 if 块中您可以调用这个方法,只需使用不同的参数而不是调用不同的方法,它们的作用相同。
否则 if(type == "miles") { MakeConversion(milesToFeetConverter); }
在所有这些操作之后,您的解决方案将如下所示:
using System;
namespace ConsoleApplication2
{
public class UnitConverter
{
double ratio;
public string From { get; set; }
public string To { get; set; }
public UnitConverter(double unitratio) { ratio = unitratio; }
public double Convert(double unit) { return unit * ratio; }
}
class Test
{
private static void MakeConversion(UnitConverter customConverter)
{
Console.WriteLine("Enter a number of {0} to be converted to {1}.",customConverter.From, customConverter.To);
double amount = Convert.ToDouble(Console.ReadLine());
string message = string.Format("{0} {1} to said number of {2}", amount, customConverter.To, customConverter.From);
Console.WriteLine(message);
}
public static void Main()
{
UnitConverter feetToInchesConverter = new UnitConverter(12) { From = "feet", To = "inches"};
UnitConverter milesToFeetConverter = new UnitConverter(5280) { From = "miles" , To = "feet"};
UnitConverter kmsToMilesConverter = new UnitConverter(1.609) { From = "kilometers", To = "miles"};
UnitConverter centToInchesConverter = new UnitConverter(2.54) { From = "centimeters", To = "inches" };
UnitConverter ozToGramsConverter = new UnitConverter(28.349) { From = "ounces", To = "grams" };
UnitConverter cupsTolitConverter = new UnitConverter(4.226) { From = "cups", To = "litters" };
string type;
int done;
done = 0;
while(done == 0)
{
{
type = Console.ReadLine();
if(type == "centi")
{
MakeConversion(centToInchesConverter);
}
else if(type == "feet")
{
MakeConversion(feetToInchesConverter);
}
else if(type == "km")
{
MakeConversion(kmsToMilesConverter);
}
else if(type == "miles")
{
MakeConversion(milesToFeetConverter);
}
else if(type == "grams")
{
MakeConversion(ozToGramsConverter);
}
else if(type == "cups")
{
MakeConversion(cupsTolitConverter);
}
else if(type == "end")
{
done = 1;
}
}
}
}
}
}
当然,您可以做更多的事情来使代码更容易和更清晰。将 if 替换为案例,将MakeConversion
方法直接替换为 UnitConverter 类,更改类的体系结构。这只是我对你能做什么的建议。但首先尝试学习如何编写你的方法和类,不要使用 VisualStudio 方法生成器,你应该知道如何自己编写方法。我以某种方式试图解释这一点,希望它有所帮助