C# 读取文本文件并将第一行与其他每行相乘
我正在做这个家庭作业,我需要读取一个整数文本文件,将数字存储到数组中。然后将每行中的数字平方(25后),然后将平方除以25,然后检查结果是否大于150 我陷入困境的地方是读取每行的数字,并像我应该的那样在我的方法中使用它们,到目前为止,我的循环和数组打印将每个数字按顺序放入文件中 非常感谢您对阵列部分的帮助,谢谢 以下是文本文件:C# 读取文本文件并将第一行与其他每行相乘,c#,arrays,split,streamreader,C#,Arrays,Split,Streamreader,我正在做这个家庭作业,我需要读取一个整数文本文件,将数字存储到数组中。然后将每行中的数字平方(25后),然后将平方除以25,然后检查结果是否大于150 我陷入困境的地方是读取每行的数字,并像我应该的那样在我的方法中使用它们,到目前为止,我的循环和数组打印将每个数字按顺序放入文件中 非常感谢您对阵列部分的帮助,谢谢 以下是文本文件: 25 150 60 63 61 70 72 68 66 68 70 以Math.Pow(60,2)/25和Math.Pow(63,2)/25为例
25 150
60
63
61
70
72
68
66
68
70
以Math.Pow(60,2)/25和Math.Pow(63,2)/25为例,依此类推。如果高于150,则打印“是”,如果低于150,则打印“否”
以下是我所拥有的:
我还有一节课
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();
}
static void Main(字符串[]args)
//声明一些变量和一个数组。
常数int MAX=50;
字符串inputLine=“”;
电阻器[]电阻器=新电阻器[最大值];
//声明一个计数器并设置为零
整数计数=0;
//这行代码获取“我的文档”文件夹的路径
字符串环境=System.environment.GetFolderPath
(System.Environment.SpecialFolder.Personal)+“\\”;
WriteLine(“电阻器批量测试分析程序”);
WriteLine(“数据文件必须在您的文档文件夹中”);
写入(“请输入文件名:”);
字符串输入=Console.ReadLine();
//将路径连接到文件名
字符串路径=环境+输入;
//现在我们可以使用完整路径来获取文档
StreamReader myFile=新的StreamReader(路径);
while(inputLine!=null)
{
inputLine=myFile.ReadLine();
if(inputLine!=null&&count
让我从您的代码中摘录几行:
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]);
}
您也可以尝试使用您的代码:
StreamReader myFile = new StreamReader(@"path_here");
const int MAX = 50;
string inputLine = "";
// Resistors[] resistor = new Resistors[MAX];
int count = 0;
int resistance = 0;
while (inputLine != null)
{
inputLine = myFile.ReadLine();
if (inputLine != null && count < MAX)
{
int inputInteger = int.Parse(inputLine);
if (count == 0) { resistance = inputInteger; }
resistor[count++] = new Resistors(inputInteger, resistance);
}
}
StreamReader myFile=newstreamreader(@“path_here”);
常数int MAX=50;
字符串inputLine=“”;
//电阻器[]电阻器=新电阻器[最大值];
整数计数=0;
内阻=0;
while(inputLine!=null)
{
inputLine=myFile.ReadLine();
if(inputLine!=null&&count
我认为这应该满足您的需要:
编辑:
根据您的注释,如果要读取第一行中的多个值,可以用逗号将它们分隔,然后继续拆分
25,150
60
63
61
70
72
68
66
68
70
您会发现,您编写的代码使用输入文件的第1行和第2行,然后使用第3行和第4行,然后使用第5行和第6行调用您的构造函数
您的描述表明您希望保留第一行,然后在每个电阻器对象的构造中使用它。也许我误解了你的问题。您可能需要考虑在预期输出的几个点上显示几个输出点。
此外,您的“inputLine.Split()”也是不必要的。您可以只解析输入行字符串 虽然我是一个支持Linq的用户,但我认为OP可能会发现它太复杂,不适合仅仅做家庭作业:)是的,哈哈,我感谢所有的帮助。这是我的第一节编程课,它只是编程的基础知识,所以这里的答案中使用的大部分代码,都是我们没有讨论过的,可能在一段时间内不会讨论的。它确实帮了很多忙。谢谢。谢谢,这确实帮了大忙,而且在很大程度上起了作用。如果在同一行中25旁边有另一个数字,我需要做什么,如果我需要除以25,然后再加50。我如何阅读同一行中的两个数字。我根据您的评论和上次问题更新更新了我的答案。告诉我是否有用。
25,150
60
63
61
70
72
68
66
68
70
static void Main(string[] args)
{
// This line of code gets the path to the My Documents Folder
string environment = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\";
Console.WriteLine("Resistor Batch Test Analysis Program");
Console.WriteLine("Data file must be in your Documents folder");
Console.Write("Please enter the file name: ");
string input = Console.ReadLine();
// concatenate the path to the file name
string path = environment + input;
// Will read all lines
var lines = File.ReadAllLines(path).ToList();
// Will get the first line arguments and split them on the comma, you add more arguments if need, just separate them by a comma
var firstLineArgs = lines[0].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(t => Convert.ToInt32(t))
.ToArray();
// Will skip the first line arguments and parse all the following numbers
var numbers = lines.Skip(1)
.Select(t => Convert.ToInt32(t))
.ToList();
// Will create each Resistors object with the first line arguments (25) and the actual number
// You can do whatever you want with the second arguments (150)
var resistors = numbers.Select(t => new Resistors(firstLineArgs[0], t))
.ToList();
Console.WriteLine("Res#\tDissipitation\tPassed");
foreach (var item in resistors)
{
// Check if item.GetPower() is greather firstLineArgs[1] (150)
// I don't know what you want to do if it's greater
Console.WriteLine("{0:d}\t{1:N}", resistors.IndexOf(item) + 1, item.GetPower());
}
Console.ReadKey();
}