簡體   English   中英

將一些經典的ASP 3.0代碼轉換為C#語言和ASP.NET

[英]Converting some classic ASP 3.0 code into C# language and ASP.NET

我需要使用ASP.NET將一些“經典ASP 3.0”代碼轉換為C#:

Randomize()
intUP  = 9
intLow =  1

intRange = intUp - intLow 
intRandom = CInt ((intRange * Rnd()) + intLow)

Response.Write(intRandom & "<br /><br />")

for i = 1 to (num) + intRandom
   Response.Write(intRandom & "<br />")
next

我已經嘗試過以下代碼:

int count;
Random rnd;

protected void Page_Load(object sender, EventArgs e)
{
    rnd = new Random();
    count = GetRandomInt(1, 9);

                for (int i = 0; i < count; i++)
                {
                   Response.Write(count.ToString());
                }
}


protected int GetRandomInt(int min, int max)
{
    return rnd.Next(min, max);
}

但是在Classic ASP 3.0中,最大輸出是9,但是在C#和ASP.NET中,它的輸出要高得多。

我想念什么?

此代碼有什么問題?

先感謝您。

實際上,最大數字在C#代碼中較低,但是您是將數字背對背寫的,因此它們顯示為一個大數字,而不是單獨的數字。

Random.Next方法返回一個數字,該數字至少與第一個參數一樣高,但小於第二個參數。 調用rnd.Next(1, 9)將給您一個1到8之間的數字。

您從零到最多比隨機數少循環一圈。 當您在循環中寫入這些數字而它們之間沒有任何內容時,最大值的輸出將是:

01234567

原始代碼會將其寫為最大的值:

9

1
2
3
4
5
6
7
8
9

要獲得之間的隨機數minmax ,添加一個到max

return rnd.Next(min, max + 1);

從一而不是零開始循環,包括最終值,並在數字之間插入一些內容:

for (int i = 1; i <= count; i++)
{
  Response.Write(count.ToString() + "<br />");
}

注意:原始代碼中的隨機計算實際上是不正確的,因為它產生的最低和最高值是其他任何數字的一半。 正確的實現是:

intRange = intUp - intLow + 1
intRandom = Int((intRange * Rnd()) + intLow)

C#代碼模仿正確的實現,而不是錯誤的實現。

暫無
暫無

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

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