【发布时间】:2018-12-05 05:53:15
【问题描述】:
我目前正在编写一个 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();
}
}
}
【问题讨论】:
-
嗨凯文。您必须在此期间仔细检查您的状况。您从字面上写了“将 studentAmount 分配给 x 或等于零”。我想你可以自己在这里提出解决方案。