简体   繁体   English

.txt文件的C#单位转换

[英]c# unit conversion from .txt file

Please can someone help? 请有人帮忙吗? I need to create a program that reads a list of units and names "eg ounce,gram,28" then asks for a user input, and then converts and displays the result. 我需要创建一个程序,该程序读取单位和名称“例如盎司,克,28”的列表,然后要求用户输入,然后转换并显示结果。 So far all i have been able to do is get it to read the first line, but nothing else. 到目前为止,我所能做的就是让它阅读第一行,但没有别的。

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Soft140AssPt3V2
{
class Program
{
static void Main(string[] args)
{
Main:
string line, Start, Finish, j, l;
double Factor, Output, Amount;
string[] SplitData = new string [2];
StreamReader Units = new StreamReader("../../convert.txt");
while ((line = Units.ReadLine()) != null)
{
SplitData = line.Split(',');
Start = SplitData[0];
Finish = SplitData[1];
Factor = Convert.ToDouble(SplitData[2]);


//Get inputs
Console.WriteLine("Please input the amount, to and from type (Ex. 5,ounces,grams):");
string Input = Console.ReadLine();
string[] Measurements = Input.Split(',', ' ', '/', '.');
Amount = Convert.ToDouble(Measurements[0]);
j = Measurements[1];
l = Measurements[2];

if (j == Start)
{
Output = (Factor * Amount);
Console.WriteLine("{0} {1} equals {2} {3}", Amount, Measurements[1], Output, Measurements[2]);
Console.ReadLine();
goto Main;
}

else
{

}

}
Units.Close();
}
}

}

For starters it looks like you are reading in the text file every time the user wants a result, and only the first line at that! 对于初学者来说,每次用户想要结果时,您似乎都在阅读文本文件,并且只有第一行!

Read the text file in and save its content somewhere, keep the conversion factors in a Dictionary: 读入文本文件并将其内容保存在某处,将转换因子保存在Dictionary中:

Dictionary<string, double> convFactors = new Dictionaty<string, double>();
while ((line = Units.ReadLine()) != null)
{
    SplitData = line.Split(',');
    string from = SplitData[0];  // should really be named 'from' not STart
    string to = SplitData[1]; // should really be named 'to' not Finish
    double factor = Convert.ToDouble(SplitData[2]); // or double.Parse ??
    convFactors.Add( from + "-" + to , factor); // ie: stores "ounce-gram", 28.0
}

Now loop reading input from the console and answering the questions: 现在循环从控制台读取输入并回答问题:

while (true);
{
    Console.WriteLine("Please input the amount, to and from type (Ex. 5,ounces,grams):");
    string Input = Console.ReadLine();
    if (Input.Equals("quit") || Input.Length == 0)
        break;
    string[] tk = Input.Split(',', ' ', '/', '.');

    double result = convFactors[tk[1] + "-" + tk[2]] * double.Parse(tk[0]);
    Console.WriteLine("{0} {1} equals {2} {3}", tk[0], tk[1], result, th[2]);
    Console.ReadLine();  // is this readline really needed??
}

Edit: and yeah - forget goto is even in the language... using a goto is a sure sign you have written a poor algorithm - well they are RARELY useful... 编辑:是的-忘记goto甚至在语言中...使用goto是确定您编写的算法较差的肯定信号-嗯,它们很少有用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM