c# - 陷入循环

标签 c#

while (true)
{
    //read in the file
    StreamReader convert = new StreamReader("../../convert.txt");

    //define variables
    string line = convert.ReadLine();
    double conversion;
    int numberIn;
    double conversionFactor;

    //ask for the conversion information
    Console.WriteLine("Enter the conversion in the form (Amount, Convert from, Convert to)");
    String inputMeasurement = Console.ReadLine();
    string[] inputMeasurementArray = inputMeasurement.Split(',');


    //loop through the lines looking for a match
    while (line != null)
    {
        string[] fileMeasurementArray = line.Split(',');
        if (fileMeasurementArray[0] == inputMeasurementArray[1])
        {
            if (fileMeasurementArray[1] == inputMeasurementArray[2])
            {
                Console.WriteLine("The conversion factor for {0} to {1} is {2}", inputMeasurementArray[1], inputMeasurementArray[2], fileMeasurementArray[2]);

                //convert to int
                numberIn = Convert.ToInt32(inputMeasurementArray[0]);
                conversionFactor = Convert.ToDouble(fileMeasurementArray[2]);

                conversion = (numberIn * conversionFactor);
                Console.WriteLine("{0} {1} is {2} {3} \n", inputMeasurementArray[0], inputMeasurementArray[1], conversion, inputMeasurementArray[2]);
                break;
            }
        }
        else
        {
            Console.WriteLine("Please enter two valid conversion types \n");
            break;
        }
        line = convert.ReadLine();
    }
}


该文件包含以下内容:

ounce,gram,28.0
pound,ounce,16.0
pound,kilogram,0.454
pint,litre,0.568
inch,centimetre,2.5
mile,inch,63360.0


用户将输入6盎司,克

这个想法是通过检查文件中的第一个和第二个单词是否与用户输入的第二个和第三个单词相同来找到正确的行。

问题在于,如果它检查第一行并且使if语句失败,则if转到else语句并停止。我试图找到一种方法,它会在找到正确的行后停止,但要等到它停下来。如果有人输入文件中未包含的值,则应该显示错误。

最佳答案

在else子句中删除break语句。这导致它退出循环。

您可以通过将转换因子读取到内部数据结构中来真正改善此代码,也许是由“ from”转换单元作为键的字典,其值是可能的输出单元及其转换因子的字典,或者是自定义键/值如果您只有一个可能的输出单元,则为一对。这将使您的内部循环变成两阶段查找(快得多),并且省去了每次都必须重新读取转换文件的麻烦。正如@Ben指出的那样,错误消息也在错误的位置。它需要在循环/查找之外,并且仅在找不到匹配项时执行。

示例代码-请注意,此代码中没有输入验证:

var conversions = new Dictionary<string,Dictionary<string,double>>();
var convert = new StreamReader("../../convert.txt");
while ((var line = convert.ReadLine()) != null)
{
    string components = line.Split(',');
    Dictionary<string,double> unitConversions;
    if (conversions.ContainsKey( components[0] ))
    {
        unitConversions = conversions[components[0]];
    }
    else
    {
        unitConversions = new Dictionary<string,double>();
        conversions.Add( components[0], unitConversions );
    }
    unitConversions.Add( components[1], Convert.ToDouble( components[2] ) );
}

while (true)
{
    //ask for the conversion information     
    Console.WriteLine("Enter the conversion in the form (Amount, Convert from, Convert to)");     
    var inputMeasurement = Console.ReadLine();     
    var inputMeasurementArray = inputMeasurement.Split(',');

    bool converted = false;
    Dictionary<string,double> unitConversion;
    if (conversions.TryGetValue(  inputMeasurementArray[1], out unitConversion ))
    {
         double conversionFactor;
         if (unitConversion.TryGetValue( inputMeasurementArray[2], out conversionFactor ))
         {
             converted = true;
             conversion = Convert.ToDouble( inputMeasurementArray[0] ) * conversionFactor;
             Console.WriteLine("{0} {1} is {2} {3} \n", inputMeasurementArray[0], inputMeasurementArray[1], conversion, inputMeasurementArray[2]);              
         }
    }

    if (!converted)
    {
         Console.WriteLine("Please enter two valid conversion types\n");
    }
}

关于c# - 陷入循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2653295/

相关文章:

c# - 使用 JSON.NET 的序列化字段的顺序

c# - 如何在文本框未满时隐藏垂直滚动条并在文本框已满时显示

c# - 将带有字符串的 C# 结构传递给 C++ 应用程序

c# - 这是从 C# 调用 C++ 函数的好方法吗?

c# - 使用 C# 为通过 Gmail 发送的邮件设置不同的 "From"地址

c# - RabbitMQ:指定端点均不可访问

c# - 陷入 C# 中循环 XML 的思路中

c# - 有什么方法可以比较 C# 中的两个列表

c# - 在 c# Winforms 中对 DataGridview 进行分组

c# - 大量数据的最佳加密(速度至关重要)?