チャレンジ:クラスの作成
メニューを表示するにはスワイプしてください
Employeeというクラスを作成し、従業員の名前、年齢、給与を格納できるコードの作成。年齢は整数値、給与は倍精度浮動小数点数で保持。
index.cs
12345678910111213141516171819using System; // Write class code below this line // Write class code above this line public class ConsoleApp { public static void Main(string[] args) { Employee e1 = new Employee(); e1.name = "Jenny"; e1.age = 32; e1.salary = 70000.0; Console.WriteLine($"{e1.name} is {e1.age} years old and she earns ${e1.salary} per year."); } }
- クラス宣言の構文は構造体宣言と非常によく似ているが、
Structキーワードの代わりにClassを使用。 - コードがコンパイルできない場合は、クラス内の各フィールドの前に
publicキーワードを付けているか確認。
index.cs
1234567891011121314151617181920212223using System; // Write class code below this line class Employee { public string name; public int age; public double salary; } // Write class code above this line public class ConsoleApp { public static void Main(string[] args) { Employee e1 = new Employee(); e1.name = "Jenny"; e1.age = 32; e1.salary = 70000.0; Console.WriteLine($"{e1.name} is {e1.age} years old and she earns ${e1.salary} per year."); } }
すべて明確でしたか?
フィードバックありがとうございます!
セクション 3. 章 3
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください
セクション 3. 章 3