我正在尝试使用我的 Documents 文件夹中的 test.txt 文件来测试这个程序。我很难找到正确的路径。有人可以给我一些想法吗?这是一项家庭作业,我几乎完成了!
using System;
using System.IO;
class Program
{
// declare constants to use in wind chill factor equation - no magic numbers
const double EQUATION_NUMBER_ONE = 35.74;
const double EQUATION_NUMBER_TWO = 0.6215;
const double EQUATION_NUMBER_THREE = 35.75;
const double EQUATION_NUMBER_FOUR = 0.4275;
const double EQUATION_EXPONENT = 0.16;
const int DEGREE_SYMBOL = 176;
static void Main()
{
// declare some variables for the main method
string fileName = "";
string line = "";
double t = 0.0;
double v = 0.0;
double wchillf = 0.0;
char degreeSymbol = (char)DEGREE_SYMBOL;
string environment = System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal) + "c:\\";
// Give student Info and ask user for a file name.
// we will read this file and use the info for our input.
Console.WriteLine("Wind Chill Calculator Braught to you by Teancum");
Console.WriteLine("for CS 1400 01X");
Console.WriteLine("11/11/2012");
Console.WriteLine("Project 8");
Console.Write("Please enter the file name in your My Documents folder: ");
fileName = Console.ReadLine();
string path = environment + fileName;
//we will create a new instance of the StreamReader class
//as well find the file in the documents folder
//this will read the Info and orginise it.
StreamReader windChillinfo = new StreamReader(path);
// start the do loop to keep readding the file untill there is no more information
do
{
// read in a line and save it as a string variable
line = windChillinfo.ReadLine();
//this if is to make sure there is no invalid info for example if the file is empty.
if (line != null)
{
string[] values = line.Split();
t = double.Parse(values[0]);
v = double.Parse(values[1]);
//here we call the windchillmagic Method
wchillf = WindChillMagic(t, v);
//here will be the Results of the windchillmagic method
Console.WriteLine("\nFor a temperature {0:f2} F{1}", t, degreeSymbol);
Console.WriteLine("\nand a wind speed of {0:f2}mph", v);
Console.WriteLine("\nThe wind chill factor would be = {0:f2}{1}\n", wchillf, degreeSymbol);
}
} while (line != null);
windChillinfo.Close();
Console.WriteLine("\nThank you for and keep Warm! Press enter to EXIT");
Console.ReadLine();
}//End Main()
static double WindChillMagic(double t, double v)
{
double wci = 0.0;
wci = EQUATION_NUMBER_ONE + (EQUATION_NUMBER_TWO * t) - (EQUATION_NUMBER_THREE * (Math.Pow(v, EQUATION_EXPONENT))) + (EQUATION_NUMBER_FOUR * t * (Math.Pow(v, EQUATION_EXPONENT)));
return wci;
}
}//End class Program