c#如何让我的程序多次运行而不设置数量

时间:2017-02-03 23:05:45

标签: c#

这是我的代码:

class Main
   {
        public string Name()
      {
        string employee = "";

             while (employee == "")
            {
            try
            {
                Console.Write("Please enter employee name: ");
                employee = Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        return employee;

    }

    public float hoursWorked()
    {
        string yesno = "";
        List<float> hWorked = new List<float>();
        float hours = 0;

        for (int i = 0; yesno == "" || yesno =="Yes"|| yesno== "yes"; i++)
        {
            try
            {
                Console.Write("Please enter hours worked: ");
                hours = float.Parse(Console.ReadLine());

                if (hours > 0)
                {
                    Console.WriteLine("Hours added");
                    hWorked.Add(hours);
                }
                else
                {
                    Console.WriteLine("Please enter valid hours");
                }

                Console.Write("Do you want to add more hours? Please state Yes or No: ");
                yesno = Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        return hWorked.Sum();

    }
}

class Program
{
    static void Main(string[] args)
    {
        Main a = new Main();

        string name = a.Name();
        float hWorked = a.hoursWorked();
        string more = "";

        Console.WriteLine("Name: {0}\nHours worked: {1}", name, hWorked);

        Console.Write("Add another employee?: ");
        more = Console.ReadLine();

        if (more == "Yes" || more == "yes")
        {
            Main b = new Main();

            string name1 = a.Name();
            float hWorked1 = a.hoursWorked()

            Console.WriteLine("Name: {0}\nHours worked: {1}", name, hWorked1);
            Console.ReadLine();
        }
        else
        {
            Console.ReadLine();
        }



    }
}

我想要做的是让用户回答问题的答案是或否#34;添加另一名员工?:&#34;我可以通过使用你在底部看到的if语句来做到这一点。如何在不预先设定添加其他员工的次数的情况下允许这样做?

3 个答案:

答案 0 :(得分:2)

代码非常质朴。我改变了你的hoursWorked方法,只需要做一些你需要的东西:

public float hoursWorked()
    {
        string yesno = "";
        List<float> hWorked = new List<float>();
        float hours = 0;

        do
        {
            try
            {
                Console.Write("Please enter hours worked: ");
                hours = float.Parse(Console.ReadLine());

                if (hours > 0)
                {
                    Console.WriteLine("Hours added");
                    hWorked.Add(hours);
                }
                else
                {
                    Console.WriteLine("Please enter valid hours");
                }

                Console.Write("Do you want to add more hours? Please state Yes or No: ");
                yesno = Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

        } while (yesno != null && yesno.ToUpper() != "NO");



        return hWorked.Sum();

    }

我没有改变任何其他事情。你可以大大改善这种逻辑。

答案 1 :(得分:1)

您可以尝试使用do

do
{
  input data
} while(some condition to continue)

MSDN documentation

答案 2 :(得分:-1)

我认为问题的一部分在于你将程序流程与你正在尝试的工作混合在一起。我做了一个快速版本,它显示了由Main控制的程序流,同时使用Employee类来封装名称和工作小时列表。

请注意,我还通过查找空字符串来简化输入,以确定何时要停止输入数据,但如果需要,可以轻松更改为使用是/否响应。

    static void Main(string[] args)
    {
        // Add new employees
        while(true)
        {
            Console.WriteLine("Please enter employee name: ");
            var employeeName = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(employeeName))
                break;

            var employee = new Employee(employeeName);

            // Add employee hours
            while(true)
            {
                Console.WriteLine("Please enter hours worked: ");
                var inputHours = Console.ReadLine();

                if (string.IsNullOrWhiteSpace(inputHours))
                {
                    Console.WriteLine("Name: {0}\nHours worked: {1}", employee.Name, employee.HoursWorked.Sum());
                    break;
                }

                var hours = float.Parse(inputHours);

                if (hours > 0)
                {
                    employee.HoursWorked.Add(hours);
                    Console.WriteLine("Hours added");
                }
                else
                {
                    Console.WriteLine("Please enter valid hours");
                }
            }
        }

    }

    public class Employee
    {
        private readonly string _name;

        public Employee(string name)
        {
            _name = name;
            HoursWorked = new List<float>();
        }

        public string Name
        {
            get { return this._name; }
        }

        public List<float> HoursWorked { get; set; }
    }