所以,我上次有一个关于语法的问题,现在语法错误已经修复,我有一个问题,即使我的教授看了之后,他也不知道如何修复。我们逐行浏览了我的代码,并通过初始的另存为对话框,一切看起来都很好,并且文件名/路径显示在调试器中。它传递到创建文件行,然后传递到我必须添加以使我的语法正常工作的代码 - 然后继续到我试图打开文件的位置,以便能够使用带有我的随机数的 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();
}
我只包括了相关的部分——事后我确实有一点,但这只是用于退出/清除按钮,调试器根本不会跳转到它们,所以我剪掉了多余的部分。