我是OpenFileDialog函数的新手,但已经找到了基础知识.我需要做的是打开一个文本文件,从文件中读取数据(仅文本)并正确地将数据放入我的应用程序中的单独文本框中.这是我在"打开文件"事件处理程序中的内容:
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog theDialog = new OpenFileDialog();
theDialog.Title = "Open Text File";
theDialog.Filter = "TXT files|*.txt";
theDialog.InitialDirectory = @"C:\";
if (theDialog.ShowDialog() == DialogResult.OK)
{
MessageBox.Show(theDialog.FileName.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
我需要阅读的文本文件是这个(对于家庭作业,我需要阅读这个确切的文件),它有一个员工编号,姓名,地址,工资和工时:
1
John Merryweather
123 West Main Street
5.00 30
Run Code Online (Sandbox Code Playgroud)
在我给出的文本文件中,有更多的员工在此之后立即以相同的格式提供信息.您可以看到员工工资和工时都在同一条线上,而不是拼写错误.
我这里有一个员工班:
public class Employee
{
//get and set properties for each
public int EmployeeNum { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public double Wage { …Run Code Online (Sandbox Code Playgroud) C++新手.在处理错误时正确循环问题.我试图检查用户输入是否是整数,并且是正数.
do{
cout << "Please enter an integer.";
cin >> n;
if (cin.good())
{
if (n < 0) {cout << "Negative.";}
else {cout << "Positive.";}
}
else
{
cout << "Not an integer.";
cin.clear();
cin.ignore();
}
}while (!cin.good() || n < 0);
cout << "\ndone.";
Run Code Online (Sandbox Code Playgroud)
输入非整数时,循环中断.我觉得像我误解的固有使用cin.clear()和cin.ignore()的状态和cin这个循环过程中.如果我删除了cin.ignore(),循环变得无限.为什么是这样?我该怎么做才能使它成为一个优雅的循环?谢谢.
我有一个方法需要根据数组的长度进行计算.我使用.length方法进行计算,但该方法使用数组的最大长度(我已声明为10)进行算术运算.这是我用来从用户获取数据的循环.我知道这不是排序数组数据的理想方法,但这是一个家庭作业,它围绕正确使用.Split方法(这不是我遇到的问题).
for (int i = 0; i < MAX; i++)
{
Console.Write("Enter a name and a score for player #{0}: ", (i + 1));
string input = Console.ReadLine();
if (input == "")
{
// If nothing is entered, it will break the loop.
break;
}
// Splits the user data into 2 arrays (integer and string).
string[] separateInput = input.Split();
name [i] = separateInput[0];
score [i] = int.Parse(separateInput[1]);
}
Run Code Online (Sandbox Code Playgroud)
这是我用来计算平均分数的方法:
static void CalculateScores(int[] score)
{
int sum = 0;
int …Run Code Online (Sandbox Code Playgroud)