C# 将变量的值转换为变量
C# convert value of variable to variable
class Program
{
struct St_test
{
public string f_name;
public string l_name;
public int age;
public string email;
}
static void proced(int number)
{
St_test s = new St_test();
Console.WriteLine("Enter the first name :");
s.f_name = Console.ReadLine();
Console.WriteLine("Enter the last name :");
s.l_name = Console.ReadLine();
agee:
Console.WriteLine("Enter the age :");
try { s.age = int.Parse(Console.ReadLine()); }
catch { Console.WriteLine("You enterd viod age"); goto agee; }
Console.WriteLine("Enter the e_mail :");
s.email = Console.ReadLine();
}
static void Main(string[] args)
{
int num;
nume:
Console.WriteLine("enter the count of people you would like to store");
try { num = int.Parse(Console.ReadLine()); }
catch { Console.WriteLine("you enterd void number"); goto nume; }
for (int i = 0; i < num; i++)
{
proced(num);
}
我想输入很多(S)给每一个(num)人。
如何重复程序(proced)并且每次重复(s)变量都有新的名字。
如果我写在程序(proced)下:
string r = "s" + number;
如何将结果字符串 (r) 转换为变量以在每个循环中使用它而不是 (s) 变量
你不能(很容易,无论如何)像那样通过名称访问变量 - 但有一个 多 更好的解决方案,即创建某种集合 - 数组或列表,例如。
我建议:
- 更改您的
St_test
结构:
- 使其成为非嵌套类型
- 给它一个更清晰的名字(例如
Person
)
- 设为class
- 不要公开字段 - 公开属性
- 可能使其不可变,在构造函数中获取所有值
- 改变你的
proced
方法:
- 将其 return 设为新
Person
- 更改名称以遵循 .NET 命名约定
- 停止使用
goto
- 将 "request an integer from the user" 分解为一个方法
- 改变你的
Main
方法:
- 创建
List<Person>
- 重复调用以前的调用
proced
。但将 return 值添加到列表
您最终会得到类似这样的代码 - 但不要盲目地复制它。确保你了解这里发生的一切。
using System;
using System.Collections.Generic;
public sealed class Person
{
public string FirstName { get; }
public string LastName { get; }
public int Age { get; }
public string Email { get; }
public Person(string firstName, string lastName, int age, string email)
{
// TODO: Validation
FirstName = firstName;
LastName = lastName;
Age = age;
Email = email;
}
}
public class Test
{
private static Person CreatePersonFromUserInput()
{
Console.WriteLine("Enter the first name:");
string firstName = Console.ReadLine();
Console.WriteLine("Enter the last name:");
string lastName = Console.ReadLine();
Console.WriteLine("Enter the age:");
int age = RequestInt32();
Console.WriteLine("Enter the email address:");
string email = Console.ReadLine();
return new Person(firstName, lastName, age, email);
}
private static int RequestInt32()
{
string text = Console.ReadLine();
int ret;
while (!int.TryParse(text, out ret))
{
Console.WriteLine("Invalid value. Please try again.");
text = Console.ReadLine();
}
return ret;
}
private static void Main()
{
Console.WriteLine("Enter the count of people you would like to store:");
int count = RequestInt32();
List<Person> people = new List<Person>();
for (int i = 0; i < count; i++)
{
people.Add(CreatePersonFromUserInput());
}
// Just to show them...
foreach (Person person in people)
{
Console.WriteLine(
$"First: {person.FirstName}; Last: {person.LastName}; Age: {person.Age}; Email: {person.Email}");
}
}
}
class Program
{
struct St_test
{
public string f_name;
public string l_name;
public int age;
public string email;
}
static void proced(int number)
{
St_test s = new St_test();
Console.WriteLine("Enter the first name :");
s.f_name = Console.ReadLine();
Console.WriteLine("Enter the last name :");
s.l_name = Console.ReadLine();
agee:
Console.WriteLine("Enter the age :");
try { s.age = int.Parse(Console.ReadLine()); }
catch { Console.WriteLine("You enterd viod age"); goto agee; }
Console.WriteLine("Enter the e_mail :");
s.email = Console.ReadLine();
}
static void Main(string[] args)
{
int num;
nume:
Console.WriteLine("enter the count of people you would like to store");
try { num = int.Parse(Console.ReadLine()); }
catch { Console.WriteLine("you enterd void number"); goto nume; }
for (int i = 0; i < num; i++)
{
proced(num);
}
我想输入很多(S)给每一个(num)人。
如何重复程序(proced)并且每次重复(s)变量都有新的名字。
如果我写在程序(proced)下:
string r = "s" + number;
如何将结果字符串 (r) 转换为变量以在每个循环中使用它而不是 (s) 变量
你不能(很容易,无论如何)像那样通过名称访问变量 - 但有一个 多 更好的解决方案,即创建某种集合 - 数组或列表,例如。
我建议:
- 更改您的
St_test
结构:- 使其成为非嵌套类型
- 给它一个更清晰的名字(例如
Person
) - 设为class
- 不要公开字段 - 公开属性
- 可能使其不可变,在构造函数中获取所有值
- 改变你的
proced
方法:- 将其 return 设为新
Person
- 更改名称以遵循 .NET 命名约定
- 停止使用
goto
- 将 "request an integer from the user" 分解为一个方法
- 将其 return 设为新
- 改变你的
Main
方法:- 创建
List<Person>
- 重复调用以前的调用
proced
。但将 return 值添加到列表
- 创建
您最终会得到类似这样的代码 - 但不要盲目地复制它。确保你了解这里发生的一切。
using System;
using System.Collections.Generic;
public sealed class Person
{
public string FirstName { get; }
public string LastName { get; }
public int Age { get; }
public string Email { get; }
public Person(string firstName, string lastName, int age, string email)
{
// TODO: Validation
FirstName = firstName;
LastName = lastName;
Age = age;
Email = email;
}
}
public class Test
{
private static Person CreatePersonFromUserInput()
{
Console.WriteLine("Enter the first name:");
string firstName = Console.ReadLine();
Console.WriteLine("Enter the last name:");
string lastName = Console.ReadLine();
Console.WriteLine("Enter the age:");
int age = RequestInt32();
Console.WriteLine("Enter the email address:");
string email = Console.ReadLine();
return new Person(firstName, lastName, age, email);
}
private static int RequestInt32()
{
string text = Console.ReadLine();
int ret;
while (!int.TryParse(text, out ret))
{
Console.WriteLine("Invalid value. Please try again.");
text = Console.ReadLine();
}
return ret;
}
private static void Main()
{
Console.WriteLine("Enter the count of people you would like to store:");
int count = RequestInt32();
List<Person> people = new List<Person>();
for (int i = 0; i < count; i++)
{
people.Add(CreatePersonFromUserInput());
}
// Just to show them...
foreach (Person person in people)
{
Console.WriteLine(
$"First: {person.FirstName}; Last: {person.LastName}; Age: {person.Age}; Email: {person.Email}");
}
}
}