2017-06-17 97 views
0
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)的人。C#將變量的值轉換爲變量

如何重複該程序(程序)並且每次重複(s)變量都有新名稱。

如果我在過程(proced)接下來寫:

string r = "s" + number; 

如何將導致串(R),以變量轉換爲用它代替(S)變量每個循環

+0

你試過使用循環,像一個while循環? – Yordan

+0

是的我用(for循環)方法把它同樣的問題 –

+0

這聽起來像你需要了解集合和數組。 (我也強烈建議不要創建像這樣的可變值類型 - 和公共字段。瞭解屬性和.NET命名約定。) –

回答

3

你不能(很容易地)通過名稱來訪問變量 - 但有更好的解決方案,例如創建某種集合 - 例如數組或列表。

我建議:

  • 改變你St_test結構:
    • 使它成爲一個非嵌套式
    • 給它一個更清晰的名稱(例如Person
    • 使它成爲一個類
    • 不要暴露字段 - 暴露屬性
    • 潛在使它不可變的,把所有的值在構造
  • 改變你proced方法:
    • 讓它返回Person
    • 將名稱更改爲遵循.NET命名約定
    • 停止使用goto
    • 將「從用戶請求整數」分解爲方法
  • 改變你Main方法:
    • 創建List<Person>
    • 多次致電曾經被稱爲proced。但增加的返回值到列表

你的代碼是這樣結束了 - 但不要盲目複製它。確保你瞭解這裏發生的一切。

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}"); 
     } 
    } 

} 
+0

非常感謝你,但我仍然在學習c# –

+1

@YoussefShaaban:這正是我給你的建議 - 幫助你學習:) –