0

所以,我上次有一个关于语法的问题,现在语法错误已经修复,我有一个问题,即使我的教授看了之后,他也不知道如何修复。我们逐行浏览了我的代码,并通过初始的另存为对话框,一切看起来都很好,并且文件名/路径显示在调试器中。它传递到创建文件行,然后传递到我必须添加以使我的语法正常工作的代码 - 然后继续到我试图打开文件的位置,以便能够使用带有我的随机数的 writeline 命令生成器 - 而不是打开适当的文件,它变成“空”作为一个值!但它并没有就此停止,它继续到随机数生成器,并滚动所需数量的随机数,但当然,由于开始值显示为“null”,它不会 t 像它应该的那样保存到文件中。哦,我教科书中的代码是产生第一个语法错误的原因,但没有提供修复它的方法。这是代码,对不起,如果它很长/难以阅读。

using System.IO; // Added to be able to use StreamWriter variable type

namespace Random_Number_File_Writer
{
public partial class Form1 : Form
{ 
    StreamWriter randomNumberFile; //Name streamwriter
    decimal numbers; //Variable to insert the number up down value into
    Random rand1 = new Random(); //Random number generator
    int writeitem; // Variable to insert random number into, to write.

    public Form1()
    {

        InitializeComponent();
    }
    public void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
    {
    }

    private void generateButton_Click(object sender, EventArgs e)
    {
        try
        {
            //Initial opening point for save file dialogue
            saveFileDialog1.InitialDirectory = @"C:\Users\Heather\Documents\Visual Studio 2010\Projects\Random Number File Writer";
            //Save As popup - Opening the file for writing usage once it's created.
            if(saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                randomNumberFile = File.CreateText(openFileDialog1.FileName);
            }
            else // Popup informing user that the data will not save to a file because they didn't save.
            {
                MessageBox.Show("You elected not to save your data.");
            }

            numbers = numericUpDown1.Value; //Gathering the number of numbers to generate from the number box.

            while (numbers > 0) // Loop counting down to 0 to give the user the appropriate number of requested random numbers.
            {

                writeitem = rand1.Next(101); // Random number generated.
                randomNumberFile.WriteLine(writeitem); //Random number written to file
                numbers--; // Initial number for user input decremented so that loop will have an ending and user only gets the amount of randoms asked for.
            }
            randomNumberFile.Close();
        }

我只包括了相关的部分——事后我确实有一点,但这只是用于退出/清除按钮,调试器根本不会跳转到它们,所以我剪掉了多余的部分。

4

2 回答 2

4

您正在使用openFileDialog1.FilenamewithFile.CreateText但高于您使用saveFileDialog1

于 2012-11-21T15:43:36.847 回答
0

对我来说,您的 while 数字循环位于保存文件对话框的 if else 逻辑之外,这对我来说毫无意义。如果他们没有选择文件,那么您为什么还要尝试将随机数写入文件?在 if 语句中移动 while 循环。

此外,正如马里奥指出的那样,您正在使用来自 2 个不同对话框的不匹配文件名,因此这是问题的根本问题,但我建议您同时修复这两个问题以避免将来出现问题。

try
    {
        //Initial opening point for save file dialogue
        saveFileDialog1.InitialDirectory = @"C:\Users\Heather\Documents\Visual Studio 2010\Projects\Random Number File Writer";
        //Save As popup - Opening the file for writing usage once it's created.
        if(saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            randomNumberFile = File.CreateText(saveFileDialog1.FileName);
            numbers = numericUpDown1.Value; //Gathering the number of numbers to generate from the number box.
            while (numbers > 0) // Loop counting down to 0 to give the user the appropriate number of requested random numbers.
            {
                writeitem = rand1.Next(101); // Random number generated.
                randomNumberFile.WriteLine(writeitem); //Random number written to file
                numbers--; // Initial number for user input decremented so that loop will have an ending and user only gets the amount of randoms asked for.
                randomNumberFile.Close();
            }
        }
        else // Popup informing user that the data will not save to a file because they didn't save.
        {
            MessageBox.Show("You elected not to save your data.");
        }
    }
于 2012-11-21T15:43:23.097 回答