好的,我正在开发一个项目,用于使用StreamReader转换测量值来读取包含以下转换列表的文件:
ounce,gram,28.0
pound,ounce,16.0
pound,Kilogram,0.454
pint,litre,0.568
inch, centimetre,2.5
mile,inch,63360.0
当程序运行时,它会要求用户输入他们想要转换的金额,然后输入想要转换的测量值,然后输入他们想要转换成的测量值。所以例如5,盎司,克
获得输入后,程序会查看文件并计算转换率,因此5盎司= 140克,因此程序将显示为最终结果
" 5盎司等于140克"
我在这个程序中遇到的问题是StreamReader只读取convert.txt文件的第一行,例如,如果我要放2,磅,盎司它会返回第一行的文字后跟一条消息" Not Matched"所以它看起来像这样:
请输入类型的数量(Ex.5,盎司,克): 2,磅,盎司 盎司,克,28.0 不匹配
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Soft140AssPt3V2
{
class Program
{
static void Main(string[] args)
{
string line, programStart, programEnd, j, l, lineTotal = "";//linetotal to take all lines from the file
double factor, endResult, amount;
string[] SplitData = new string[2];
string[] fileLines;
StreamReader unitsOfMeasurement = new StreamReader("../../convert.txt"); //Reads the convert.txt file
Console.WriteLine("convert.txt has uploaded");
while ((line = unitsOfMeasurement.ReadLine()) != null)
{
lineTotal += line + "\n";
}
fileLines = lineTotal.Split('\n');//place file lines in an array of string
//Get inputs
Console.WriteLine("\nPlease input the amount, to and from type (Ex. 5,ounces,grams):");
string userInput = Console.ReadLine();
for (int i = 0; i < fileLines.Length - 1; i++)
{
Console.WriteLine(fileLines[i]);
SplitData = fileLines[i].Split(',');
programStart = SplitData[0];
programEnd = SplitData[1];
factor = Convert.ToDouble(SplitData[2]);
string[] filter = userInput.Split(',', ' ', '/', '.');
amount = Convert.ToDouble(filter[0]);
j = filter[1];
l = filter[2];
if (j == programStart)
{
endResult = (factor * amount);
Console.WriteLine("\n{0} {1} equals {2} {3}", amount, filter[1], endResult, filter[2]);
Console.ReadLine();
}
else
{
Console.WriteLine("Not Matched");
Console.ReadLine();
}
}
unitsOfMeasurement.Close();
}
}
}
答案 0 :(得分:1)
解决方案1:
StringBuilder lineTotal = new StringBuilder();
while ((line = unitsOfMeasurement.ReadLine()) != null)
{
lineTotal.Append(line + Environment.NewLine);
}
fileLines = lineTotal.ToString().Split(new string[]{Environment.NewLine},
StringSplitOptions.RemoveEmptyEntries);
解决方案2:
StringBuilder lineTotal = new StringBuilder();
foreach(var line in File.ReadLines("../../convert.txt"))
{
lineTotal.Append(line);
}
fileLines = lineTotal.ToString().Split(new string[]{Environment.NewLine},
StringSplitOptions.RemoveEmptyEntries);
解决方案3:
string fileName = "../../convert.txt";
var lines = File.ReadLines(fileName).Where(line =>
!string.IsNullOrWhiteSpace(line)).ToArray();
答案 1 :(得分:0)
以下是优化代码(您可以根据需要进一步修改代码):
static void Main(string [] args) { 尝试 { 双重因素,endResult,金额; string [] fileLines;
StreamReader unitsOfMeasurement = new StreamReader("../../convert.txt"); //Reads the convert.txt file
Console.WriteLine("convert.txt has uploaded");
fileLines = unitsOfMeasurement.ReadToEnd().Replace("\r\n", "\n").Split('\n');//place file lines in an array of string
//Get inputs
Console.WriteLine("\nPlease input the amount, to and from type (Ex. 5,ounce,gram):");
string userInput = Console.ReadLine();
bool blnFound = false;
for (int i = 0; i < fileLines.Length; i++)
{
string[] filter = userInput.Split(',');
if ((filter.Length == 3) && (filter[1].Trim() == fileLines[i].Split(',')[0].Trim()))
{
blnFound = true;
Console.WriteLine(fileLines[i]);
factor = Convert.ToDouble(fileLines[i].Split(',')[2]);
amount = Convert.ToDouble(filter[0]);
endResult = (factor * amount);
Console.WriteLine("\n{0} {1} equals {2} {3}", amount, filter[1], endResult, filter[2]);
Console.ReadLine();
break;
}
}
if (blnFound == false)
{
Console.WriteLine("Not Matched");
Console.ReadLine();
}
unitsOfMeasurement.Close();
}
catch (Exception ex)
{
throw (ex);
}
}