且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何只允许有限数量的用户登录?

更新时间:2023-12-03 19:43:10

正如亨利所说,这没有任何意义.
但是,我认为这里有足够的信息可以说明您的问题是什么:
当需要if语句时,正在使用for循环.

如果设置一个for循环,它将多次执行相同的代码:这不是您的描述所说的.

当用户要登录时,请检查用户数".如果超过或等于最大值,则拒绝登录.否则,请增加用户数量并登录.
As Henry says, that doesn''t make a lot of sense.
However, I think there is enough there to tell what your problem is:
You are using a for loop, when you want an if statement.

If you set up a for loop, it will do the same code several times: that is not what your description says.

When a user wants to log in, check the "number of users". If this exceeds or equals the maximum, then refuse the login. Otherwise, increase the number of users, and log them in.
if (count >= noofusers)
   {
   // Refuse login
   ...
   }
else
   {
   count++;
   ...
   }



请注意,您将需要一些处理注销和死"用户的方法:例如,如果PC在没有注销的情况下重新启动,则需要终止用户登录并以某种方式减少计数.



Note that you will need something to cope with logout, and with "dead" users: for example if the PC gets re-booted without a log out you need to kill the user login and reduce the count somehow.