簡體   English   中英

c#do-while循環如何重置變量?

[英]c# do-while loop how to reset a variable?

 int line = 0;///i want to reset this back to 0///
    private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
        int y = 0;
        Console.WriteLine("1st " + line);
        do
        {
            e.Graphics.DrawString(invoiceList[line], new Font("Arial", 10, FontStyle.Regular), Brushes.Black, 50, 200);
            Console.WriteLine("2nd " + line);
            line += 1;
            Console.WriteLine("3rd " + line);
            y += 1;

            if (y > 0)
            {
                e.HasMorePages = line != invoiceList.Count();

                Console.WriteLine("4th " + line);
                break;   
            }  
        } while (line < invoiceList.Count());
        Console.WriteLine("6th " + line);
    }

如何重置變量line 因為當我按下printPreviewDialog的打印按鈕時,它只會繼續printPreviewDialog結果是System.ArgumentOutOfRangeException

編輯1:抱歉,我不清楚。 它的這一行e.Graphics.DrawString(invoiceList[line], new Font("Arial", 10, FontStyle.Regular), Brushes.Black, 50, 200); 那給我一個錯誤。

所以首先這是當我按下打印按鈕時發生的情況

從圖片“ 6th( line )= 12”或索引12中可以看到。

因此,當我再次從打印預覽中按打印按鈕以打印物理副本時,它將顯示此內容 對不起,我真的很無能為力。

您必須在printDocument1_PrintPage聲明變量。 以便每次將其初始化為0 因此事件簽名可能看起來像:

  private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
      int y = 0, line = 0;
      //Rest of code here
    }

或者,您可以在循環后將其重置為0 ,如果是這樣,則代碼段將類似於:

int line = 0;
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
 {
    int y = 0;
    // code here
    Console.WriteLine("6th " + line);
    line=0; for the next time the value starts from 0
 }

如果您需要i在外部范圍內,則必須重置i = 0;否則,請重新設置i = 0; 內部范圍內的某個地方。

Console.WriteLine("6th " + line); 您可以在此之后立即添加一行,然后說i = 0;

這樣,您的代碼就可以完成您要問的事情,而您仍然擁有i外部作用域。

//設為0

        if (y > 0)
        {
            e.HasMorePages = line != invoiceList.Count();

            Console.WriteLine("4th " + line);
            break;   
        }  
    } while (line < invoiceList.Count());
    Console.WriteLine("6th " + line);
   line = 0;
}
private void printDocument1_BeginPrint(object sender, PrintEventArgs e)
    {
        line = 0;
    }

伙計們,謝謝您的時間,我知道了:P。

我使用該程序進行排序,希望它能對某人有所幫助。

static void SortArray()
    {
        int[] array = { 40, 5, 6, 1, 90, 23, 5 };
        //Array.Sort(array);
        int i = 0;

        while (i < array.Length)
        {
            bool flag = false;
            if (i < array.Length - 1)
            {
                if (array[i] > array[i + 1])
                {
                    int temp = array[i];
                    array[i] = array[i + 1];
                    array[i + 1] = temp;
                    //reset i
                    i = 0;
                    flag = true;
                }
            }
            if (!flag)
            {
                i++;
            }
        }
        //PRINT
        for (int a = 0; a < array.Length; a++)
        {
            Console.WriteLine(array[a].ToString());
        }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM