剛接觸方法這塊,很多不明白,求大佬幫幫忙
uj5u.com熱心網友回復:
老師的要求:
using System;
namespace Study {
class Program {
static void Main() {
string[] name = new string[5];
int[] monthly = new int[5];
for (int i = 0; i < 5; i++) {
Console.WriteLine("第" + i + 1 + "位員工資訊:");
Console.Write("姓名:");
name[i] = Console.ReadLine();
Console.Write("薪資:");
monthly[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine("請輸入要查看的員工編號:");
int num = int.Parse(Console.ReadLine());
Show(name[num - 1], monthly[num - 1]);
}
static void Show(string n, int m) {
Console.WriteLine(n + "的年薪為" + m * 12);
}
}
}
實際需求正常一點的寫法:
using System;
using System.Collections.Generic;
namespace Study {
class Program {
static void Main() {
HumanResource hr = new HumanResource();
for (int i = 0; i < 5; i++) {
Console.WriteLine($"第{i + 1}位員工資訊:");
Console.Write("姓名:");
string name = Console.ReadLine();
Console.Write("薪資:");
int monthly = int.Parse(Console.ReadLine());
hr.Add(name, monthly);
}
Console.WriteLine("請輸入要查看的員工編號:");
int num = int.Parse(Console.ReadLine());
hr.Show(num - 1);
}
}
class HumanResource {
private List<Human> humans = new List<Human>();
public void Add(string name,int monthly) {
humans.Add(new Human { Name = name, Monthly = monthly });
}
public void Show(int num) {
Console.WriteLine($"{humans[num].Name}的年薪為{humans[num].Monthly * 12}");
}
}
class Human {
public string Name { get; set; }
public int Monthly { get; set; }
}
}
uj5u.com熱心網友回復:
namespace Example
{
class Program
{
static void Main(string[] args)
{
string[] nameArray = new string[5] { "李白", "高漸離", "程交金", "安琪拉", "狄仁杰" };
int[] salaryArray = new int[5] { 8220, 7800, 6800, 5600, 8400 };
for (int i = 1; i <= 5; i++)
{
string name = nameArray[i - 1];
int salary = salaryArray[i - 1];
Console.WriteLine("第{0}位員工資訊:\n姓名:{1}\n薪資:{2}", i, name, salary);
}
Console.WriteLine("請輸入要查看的員工編號:");
int num = Convert.ToInt32(Console.ReadLine()); //try-catch
string nameStr = nameArray[num - 1];
int salaryStr = salaryArray[num - 1];
PrintResult( nameStr, salaryStr);
}
static void PrintResult(string name, int salary)
{
Console.WriteLine("{0}的年薪為{1}", name, salary * 12);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/62022.html
標籤:C#
