2017-10-27 7 views
0

この問題を解決する方法を見つけようとサイトを掘り下げました。私はC#の初心者ですから、初心者の質問にお答えしたら謝ります。私は従業員クラスと2つの新しい従業員インスタンスを作成しましたが、最初の従業員情報のみが動作します

私達はちょうど私があまりにも面倒見つからないもの、私たちのコースで授業を始めました。しかし、私たちの最初の割り当てでは、従業員の賃金引き上げ率とパーセンテージを取るクラスEmployeeを作成しました。メインプログラムでは、2つの新しいインスタンスe1とe2を作成します。私は、GetValueメソッドを使用して従業員のデータを読み込みます。私の問題は、それは1人目の従業員にとって素晴らしいことですが、2人目の従業員の入力を求めないことです。 e1とe2の両方のデータを読み込んで出力するにはどうすればよいですか?

私は両方の従業員のための入力を取得するために行うことができますいくつかのループまたは反復はありますか?事前に

おかげで、 ダイアン

は、ここに私のコードです:

//identify the variables 
     string firstName, lastName, employeeID, department; 
     decimal salary; 

     firstName = GetValue("First Name"); 
     lastName = GetValue("Last Name"); 
     employeeID = GetValue("Employee ID"); 
     department = GetValue("Employee Department"); 
     salary = Convert.ToDecimal(GetValue("Salary")); 
     //create two employee objects 

     Employee e1 = new Employee(firstName, lastName, employeeID, salary, department); 
     Employee e2 = new Employee(firstName, lastName, employeeID, salary, department); 

     //get user input 

     //clear the screen 
     Console.Clear(); 

     //Display the pay rise information 

     Console.WriteLine("Total increase in salary for employee 1 is {0}", e1); 

     Console.WriteLine("Total increase in salary for employee 2 is {0}", e2); 
     Console.ReadLine(); 

    }//end of Main 


    static public string GetValue(string value) 
    { 
     Console.WriteLine("Please enter the {0} >>", value); 
     string input = Console.ReadLine(); 
     return input; 

    }//end GetValue 
+2

あなたは両方の 'Employee'に全く同じ値を指定しています - あなたは2番目の' Employee'の新しい入力を要求していません。 – UnholySheep

答えて

1

確かに、あなただけのループにおける情報収集部分をラップする必要があります。ここに簡単な例があります。下の例では、従業員オブジェクトをリストに追加しているため、個々の個別参照を保持する必要はありません。

var employees = new List<Employee>(); 

// Populate our list with two employees 
for (int i = 0; i < 2; i++) 
{ 
    var firstName = GetValue("First Name"); 
    var lastName = GetValue("Last Name"); 
    var employeeID = GetValue("Employee ID"); 
    var department = GetValue("Employee Department"); 
    var salary = Convert.ToDecimal(GetValue("Salary")); 

    employees.Add(new Employee(firstName, lastName, employeeID, salary, department)); 
} 

// Display the employee information: 

var counter = 1; 
foreach (var employee in employees) 
{ 
    Console.WriteLine("Total increase in salary for employee {0} is {1}", 
     counter++, employee); 
} 
0

従業員の作成は、このシナリオでは別の方法の候補です。

string firstName, lastName, employeeID, department; 
decimal salary; 

firstName = GetValue("First Name"); 
lastName = GetValue("Last Name"); 
employeeID = GetValue("Employee ID"); 
department = GetValue("Employee Department"); 
salary = Convert.ToDecimal(GetValue("Salary")); 

Employee e1 = new Employee(firstName, lastName, employeeID, salary, department); 

次のような方法で作成できます:具体的には、このコードは二回呼び出されるべき

あなたの主な方法で、次に

static Employee ReadEmployeeFromInput() 
{ 
    //... all the code from the above snippet; 
    return e1; 
} 

を、二人の従業員をお読みください。

Employee e1 = ReadEmployeeFromInput(); 
Employee e2 = ReadEmployeeFromInput(); 

これを繰り返すことで、従業員数を増やすことができます。ループ内など。

関連する問題