C# 打印保存在数组c中的名称和密码#
所以我创建了这个,数据应该保存在一个数组中。但是我尝试使用print并将数字放入数组来打印出来,但是没有成功。我使用Console.WriteLine打印保存在数组中的字符串,但根本不起作用。 我是新手,所以帮帮我C# 打印保存在数组c中的名称和密码#,c#,C#,所以我创建了这个,数据应该保存在一个数组中。但是我尝试使用print并将数字放入数组来打印出来,但是没有成功。我使用Console.WriteLine打印保存在数组中的字符串,但根本不起作用。 我是新手,所以帮帮我 public static class Program { public static void newid() { string username = null; string passwd = null; Usern p = new Us
public static class Program
{
public static void newid()
{
string username = null;
string passwd = null;
Usern p = new Usern();
Console.WriteLine("enter your name:");
p.Name = Console.ReadLine();
username = p.Name;
UserP p1 = new UserP();
Console.WriteLine("enter password:");
p1.Password = Console.ReadLine();
passwd = p1.Password;
}
public static void Main()
{
Console.WriteLine("how many account you want to create :");
int num = Convert.ToInt32(Console.ReadLine());
int[] numarr = new int[num];
for (int a = 0; a<num; a++)
{
Console.WriteLine("account = {0}",a);
newid();
}
int cc = Convert.ToInt32(Console.ReadLine());
int ddd = numarr;
Console.WriteLine("{0}");
}
}
公共静态类程序
{
公共静态void newid()
{
字符串username=null;
字符串passwd=null;
Usern p=new Usern();
Console.WriteLine(“输入您的姓名:”);
p、 Name=Console.ReadLine();
用户名=p.名称;
UserP p1=新的UserP();
Console.WriteLine(“输入密码:”);
p1.Password=Console.ReadLine();
passwd=p1.密码;
}
公共静态void Main()
{
Console.WriteLine(“要创建多少帐户:”);
int num=Convert.ToInt32(Console.ReadLine());
int[]numar=新的int[num];
对于(int a=0;a您应该创建如下类:
class User {
public string Name;
public string Password;
}
然后,您应该更改newid()以创建并返回User
实例
最后,您应该将创建的实例存储在用户数组中:User[]users=new User[num];
尝试所有这些,然后返回给我们。这里有一些建议,希望能帮助您获得一个工作版本
以你的代码为荣,尊重用户。使用大写和标点符号
Console.WriteLine("How many accounts would you like to create? Please enter a number and press enter:");
使数组包含存储用户名和密码的类型。它可以是类,甚至是简单的元组
var users = new (string Username, string Passw0rd)[num];
(请注意,在实际应用程序中,密码很少以纯文本形式存储。)
更改newid
以返回要存储在数组中的数据
static (string Name, string Passw0rd) GetAccount()
{
string username = null;
string passwd = null;
Console.WriteLine("enter your name:");
username = Console.ReadLine();
Console.WriteLine("enter password:");
passwd = Console.ReadLine();
return( username, passwd );
}
调用重构的newid
/GetAccount
,并将返回值存储在数组中
for (int i = 0; i<num; i++)
{
Console.WriteLine("account = {0}",i);
users[i] = GetAccount();
}
for(int i=0;i您确定在测试代码时您的数组num
不是空的吗?等等;什么数组?唯一的数组是一个整数数组….?旁注:在任何远程“真实”的数组中,您都不会存储密码(除非您正在编写密码管理软件等,并且在这种情况下它是加密的)但是:什么是Usern
和UserP
?似乎您需要一个具有两个属性的User
类,以及一个列表
?Console.WriteLine(stribg.format(“account={0}”,a));@GoodSamaritan,它只会写帐户数;坦率地说,如果你想这样做,num
将是比a
更好的选择,因为至少很明显,这段代码甚至无法编译。你不能将整数数组分配给单个整数
for (int i = 0; i<users.Length; i++)
{
Console.WriteLine($"User {i}");
Console.WriteLine($"Name: {user[i].Username}");
}