【问题标题】:Working with Files, loop not stopping where i want to使用文件,循环不会在我想要的地方停止
【发布时间】: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 或等于零”。我想你可以自己在这里提出解决方案。

标签: c# loops


【解决方案1】:

当这不再正确时,循环停止

while ((x = studentAmount) >= 0);

你永远不会改变 studentAmount,所以循环将永远运行

你在某处需要studentAmount--

会更简单明了

for(int i = 0; i < studentAmount; i++)
{
   .....
}

而不是你的 do / while 循环

【讨论】:

  • 或者++x,如果这是OP对该变量的意图。我 100% 同意 for 循环在这里更有意义。循环的意图在顶部说明,并自动递增。
【解决方案2】:

Kevin - 您发布的代码无法按原样运行 - 您忘记了“控制台”。在读/写之前。您还应该验证输入 - 但您的具体问题是您没有终止循环。请看下面代码中的cmets:

    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();
    }

【讨论】:

    【解决方案3】:

    欢迎来到 SO Kevin。看起来你只是在学习编程。正如 Pm100 已经提到的那样;您正在进入称为无限循环的循环。 对于您的解决方案,只需替换这些行。

        double x = 0;
            do
            {
                .....
                x=x+1;  //Add this line
            } while (x < studentAmount);  //Change the condition like this
    

    【讨论】:

    • 非常感谢,这是一个很大的帮助!
    猜你喜欢
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    相关资源
    最近更新 更多