【发布时间】: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#