我目前正在研究C#程序,该程序要求我编写一个程序以接收有关多个学生的信息,然后将输入的数据存储到两个文件中:一个文本文件和一个二进制文件。编写程序,以便在运行程序时,程序会询问用户要输入的学生数,然后程序询问有关该学生数的数据(学生姓名,学生身高,学生体重)输入后,数据将存储在两个文件中:一个文本文件和一个二进制文件。现在的问题是,无论何时运行循环,循环都不会停止在我希望的位置,这是用户一开始输入的数量。如果您有任何帮助,请随时发表评论。 谢谢。 这是代码:
using System;
using static System.Console;
using System.IO;
namespace BinaryAssignment
{
class Program
{
static void Main(string[] args)
{
StreamWriter outputFile;
FileStream fsOutput = new FileStream("outFile.bin", FileMode.Create);
BinaryWriter myOutputFile = new BinaryWriter(fsOutput);
outputFile = new StreamWriter("ex1.txt");
outputFile = new StreamWriter("ex1.bin");
double studentAmount ;
string studentName;
int studentHeight;
double studentWeight;
Write("Data of how many students do you want to enter? ");
studentAmount = double.Parse(ReadLine());
double x = 0;
do
{
Write("Enter the name of the student: ");
studentName = ReadLine();
myOutputFile.Write(studentName);
outputFile.WriteLine(studentName);
Write("Enter the height of the student in centimeters: ");
studentHeight = int.Parse(ReadLine());
outputFile.WriteLine(studentHeight);
myOutputFile.Write(studentHeight);
Write("Enter the weight of the student in kilograms: ");
studentWeight = double.Parse(ReadLine());
outputFile.WriteLine(studentWeight);
myOutputFile.Write(studentWeight);
} while ((x = studentAmount) >= 0);
outputFile.Close();
Console.ReadKey();
}
}
}
答案 0 :(得分:1)
当这种情况不再成立时,您的循环就会停止
while ((x = studentAmount) >= 0);
您永远不会更改studentAmount,因此循环将永远运行
您需要在某个地方studentAmount--
更简单明了
for(int i = 0; i < studentAmount; i++)
{
.....
}
代替您的do / while循环
答案 1 :(得分:0)
欢迎来到SO Kevin。看起来您只是在学习编程。正如Pm100所述;您正在遇到称为无限循环的循环。 对于您的解决方案,只需替换这些行。
double x = 0;
do
{
.....
x=x+1; //Add this line
} while (x < studentAmount); //Change the condition like this
答案 2 :(得分:0)
Kevin-您发布的代码无法按原样运行-您忘记了“控制台”。在读/写之前。您还应该验证输入-但您的特定问题是您没有终止循环。请在下面的代码中查看注释:
static void Main(string[] args)
{
StreamWriter outputFile;
FileStream fsOutput = new FileStream("outFile.bin", FileMode.Create);
BinaryWriter myOutputFile = new BinaryWriter(fsOutput);
outputFile = new StreamWriter("ex1.txt");
outputFile = new StreamWriter("ex1.bin");
int studentAmount; // This should be an int not double
string studentName;
int studentHeight;
double studentWeight;
Console.Write("Data of how many students do you want to enter? ");
// You should validate the input using the appropriate TryParse
var input = Console.ReadLine();
if (!int.TryParse(input, out studentAmount))
{
// You need to append 'Console.' to the beginning of the read/write operations
Console.WriteLine("You entered an invalid number for student amounnt - ending...");
Console.ReadKey();
return;
}
// double x = 0; // You don't need this variable
do
{
Console.Write("Enter the name of the student: ");
studentName = Console.ReadLine();
myOutputFile.Write(studentName);
outputFile.WriteLine(studentName);
Console.Write("Enter the height of the student in centimeters: ");
input = Console.ReadLine();
if (!int.TryParse(input, out studentHeight))
{
Console.WriteLine("You entered an invalid number for height - ending...");
Console.ReadKey();
return;
}
outputFile.WriteLine(studentHeight);
myOutputFile.Write(studentHeight);
Console.Write("Enter the weight of the student in kilograms: ");
input = Console.ReadLine();
if (!double.TryParse(input, out studentWeight))
{
Console.WriteLine("You entered an invalid number for weight - ending...");
Console.ReadKey();
}
outputFile.WriteLine(studentWeight);
myOutputFile.Write(studentWeight);
// You need to decrement your counter so you don't loop forever
} while (--studentAmount > 0);
outputFile.Close();
Console.ReadKey();
}