简体   繁体   English

将一些经典的ASP 3.0代码转换为C#语言和ASP.NET

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

I need to convert some "Classic ASP 3.0" code into C# using 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

And I have tried this code: 我已经尝试过以下代码:

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

But in Classic ASP 3.0, the maximum output is 9, but in C# and ASP.NET, it is much higher. 但是在Classic ASP 3.0中,最大输出是9,但是在C#和ASP.NET中,它的输出要高得多。

What am I missing? 我想念什么?

What's wrong with this code? 此代码有什么问题?

Thank you in advance. 先感谢您。

Actually the maximum number is lower in the C# code, but you are writing the numbers back to back so they are shown as one big number instead of separate numbers. 实际上,最大数字在C#代码中较低,但是您是将数字背对背写的,因此它们显示为一个大数字,而不是单独的数字。

The Random.Next method returns a number that is at least as high as the first parameter, and lower than the second parameter. Random.Next方法返回一个数字,该数字至少与第一个参数一样高,但小于第二个参数。 Calling rnd.Next(1, 9) will give you a number between 1 and 8. 调用rnd.Next(1, 9)将给您一个1到8之间的数字。

You are loopin from zero and up to one less than the random number. 您从零到最多比随机数少循环一圈。 When you write those number in the loop with nothing in between them, the output for the largest value would be: 当您在循环中写入这些数字而它们之间没有任何内容时,最大值的输出将是:

01234567

where the original code would instead write this for the largest value: 原始代码会将其写为最大的值:

9

1
2
3
4
5
6
7
8
9

To get a random number between min and max , add one to the max : 要获得之间的随机数minmax ,添加一个到max

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

Loop from one instead of zero, include the end value, and put something in between the numbers: 从一而不是零开始循环,包括最终值,并在数字之间插入一些内容:

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

Note: The random calculation in the original code is actually incorrect, as it would generate the lowest and the highest value half as often as any of the other numbers. 注意:原始代码中的随机计算实际上是不正确的,因为它产生的最低和最高值是其他任何数字的一半。 The correct implementation would be: 正确的实现是:

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

The C# code mimics the correct implementation, not the incorrect one. C#代码模仿正确的实现,而不是错误的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM