【发布时间】:2016-04-18 01:22:08
【问题描述】:
我正在做这个作业,我需要读取一个整数文本文件,将数字存储到数组中。然后将每行中的数字平方(25之后),然后将平方除以25,然后检查结果是否大于150
我卡住的地方是读取每一行的数字并像我应该的那样在我的方法中使用它们,到目前为止,我的循环和数组打印将每个数字按顺序放入文件中。
非常感谢有关数组部分的任何帮助,谢谢。
这是文本文件:
25 150
60
63
61
70
72
68
66
68
70
所以,以Math.Pow(60,2) / 25 和Math.Pow(63,2) / 25 等等为例。然后如果高于 150,则打印“yes”,如果低于 150,则打印“no”
这是我所拥有的: 我还有一门课
class Resistors
{
//declare variables for the resistance and the volts.
private int resistance;
private int volts;
public Resistors(int p1, int p2)
{
resistance = p2;
volts = p1;
}
//GetPower method.
//purpose: to get calculate the power dissipation of the resistor.
//parameters: it takes two intigers.
//returns: the total power as a double.
public double GetPower()
{
return (Math.Pow(volts, 2) / resistance);
}
}
剩下的就是这里了。
static void Main(string[] args)
//declare some variables and an array.
const int MAX = 50;
string inputLine = "";
Resistors[] resistor = new Resistors[MAX];
//declare a counter and set to zero
int count = 0;
// This line of code gets the path to the My Documents Folder
string environment = System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal) + "\\";
WriteLine("Resistor Batch Test Analysis Program");
WriteLine("Data file must be in your Documents folder");
Write("Please enter the file name: ");
string input = Console.ReadLine();
// concatenate the path to the file name
string path = environment + input;
// now we can use the full path to get the document
StreamReader myFile = new StreamReader(path);
while (inputLine != null)
{
inputLine = myFile.ReadLine();
if (inputLine != null && count < MAX)
{
string[] data = inputLine.Split();
int dataR = int.Parse(data[0]);
string[] pie = inputLine.Split();
int pieV = int.Parse(pie[0]);
resistor[count++] = new Resistors(dataR, pieV);
}
}
WriteLine("Res#\tDissipitation\tPassed");
for (int j = 0; j < count; j++)
{
WriteLine("{0:d}\t{1:N}", j + 1, resistor[j].GetPower());
}
ReadKey();
}
【问题讨论】:
标签: c# arrays split streamreader