0

我想制作一个随机数并将其包含在一个整数中......这是我当前的代码

Random RandString = gcnew Random();
             random = (2 * RandString.Next(1 / 2, 100 / 2));
                 if (random >= 1 || random <= 35)
                 {
                     Friend = 1;
                     FriendName = "RainFall";
                 }
                 if (random >= 36 || random <= 65)
                 {
                     Friend = 2;
                     FriendName = "TempoDrop";
                 }
                 if (random >= 66 || random <= 99)
                 {
                     Friend = 3;
                     FriendName = "HeartFelt";
                 }
                 if (random == 100)
                 {
                     Friend = 4;
                     FriendName = "SwagMasta";
                 }

但是每当我尝试这个时,我都会收到这个错误......

1>------ Build started: Project: Retaliation, Configuration: Debug Win32 ------
1>  Form2.cpp
1>c:\users\devon\documents\visual studio   2010\projects\retaliation\retaliation\Form1.h(358): error C2664:    'System::Random::Random(int)' : cannot convert parameter 1 from 'System::Random ^' to 'int'
1>          No user-defined-conversion operator available, or
1>          There is no context in which this conversion is possible
1>  Retaliation.cpp
1>c:\users\devon\documents\visual studio   2010\projects\retaliation\retaliation\Form1.h(358): error C2664:   'System::Random::Random(int)' : cannot convert parameter 1 from 'System::Random ^' to 'int'
1>          No user-defined-conversion operator available, or
1>          There is no context in which this conversion is possible
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我究竟做错了什么?我能做些什么来解决这个问题?

4

1 回答 1

0

当您使用gcnew运算符时,它会返回一个引用,因此您只需添加^这样的

Random^ RandString = gcnew Random();
于 2013-06-02T06:21:47.063 回答