【问题标题】:What is the mistake in this code for generating a random password? [closed]这段代码生成随机密码的错误是什么? [关闭]
【发布时间】:2021-12-30 12:44:47
【问题描述】:

我编写了这段代码来生成随机密码。当我运行它时,我收到一条错误消息,说 public Main() 方法应该在公共类中。

using System;

namespace Rnadom_Password_generator
{
    class Program
    {
        void Main()
        {
            string[] allCharacters = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "U", "V", "W", "Q", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "7", "9", "0", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "=", "+", "*", "/"};
            string password = "";

            Random character = new Random();
            int passwordLength = 10;

            for (int i = 0; i < passwordLength; i++)
            {
                int rnadomNum = character.Next(0, allCharacters.Length);
                password += allCharacters[rnadomNum];
                Console.WriteLine(password);
            }
        }
    }
}

我该怎么办?

【问题讨论】:

  • 创建类public并将main声明为public static void Main(string[] args)
  • 另外,不要使用Random 作为密码,它不是加密安全的。
  • 您得到的real 错误是“程序不包含适合入口点的静态 Main 方法”。按照这些说明解决问题。
  • 抱歉,但是阅读此public Main() method is supposed in public class 是什么让您认为生成随机密码的代码有问题?恕我直言,这条消息清楚地描述了某些事情被宣布为错误,即方法 Main 需要在公共类中。
  • @RandRandom(还有 Sean)方法和类不需要是公共的,唯一需要的修改是在方法中添加静态。甚至不需要争论。

标签: c#


【解决方案1】:

确保您的主要方法是静态的。像这样。

using System;

namespace Rnadom_Password_generator
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] allCharacters = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "U", "V", "W", "Q", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "7", "9", "0", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "=", "+", "*", "/" };
            string password = "";

            Random character = new Random();
            int passwordLength = 10;

            for (int i = 0; i < passwordLength; i++)
            {
                int rnadomNum = character.Next(0, allCharacters.Length);
                password += allCharacters[rnadomNum];
                Console.WriteLine(password);
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 2019-06-04
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    相关资源
    最近更新 更多