2

I'm not too sure on what type of loops to use, the class power point isn't really getting the point across and I have no idea what to do

      {
        string name;
        double sales;

        //start loop here to ask user for employee info

        name =GetName("name");
        //Start a loop here to ask for the sales of each day
        sales1 = GetSales("day 1 sales");
        sales2 = GetSales("day 2 sales");
        sales3 = GetSales("day 3 sales");
        sales4 = GetSales("day 4 sales");
        sales5 = GetSales("day 5 sales");
        Console.WriteLine("\n");
        //End loop here asking for sales of each day 

        Employee emp1 = new Employee( );


        Display(emp1);

//End loop here asking for employee info

In a previous program we have used

    {
        name = GetInput("name");
        grade1 = int.Parse(GetInput("first grade"));
        grade2 = int.Parse(GetInput("second grade"));
        grade3 = int.Parse(GetInput("third grade"));
    }

    public static string GetInput(string temp)
    {
        Console.Write("Please enter your {0}: ", temp);
        return Console.ReadLine();
    }

to get the user to input multiple numbers and a name but my prof wants this in a loop but I have no idea how to convert this into a loop

4

4 回答 4

2

将字符串放入数组并使用 for 循环

于 2013-11-13T00:36:13.210 回答
2

我会使用一个字符串数组for 循环,如下所示:

string name = "name" ;
string names = "names";
string named = "named";
string nameds = "nameds";

string[] strArray = new string[] { name, names, named, nameds };

for (int i = 0; i < strArray.Length; i++)
{
   MessageBox.Show(strArray[i].ToString());
}

注意:您需要将变量更改为符合您要求的变量,这MessageBox只是为了向您显示字符串已填充到数组中并通过for loop.

于 2013-11-13T00:40:25.327 回答
1

这将适合一名员工

double sales[] = new double[5];

for(int i = 0; i < 5; i++) {
    sales[i] = GetSales("day " + (i + 1) + " sales");
    Console.WriteLine("\n");
}

Employee emp = new Employee();

扩展此解决方案以满足您的需求。

于 2013-11-13T00:53:10.023 回答
1

我会使用一个while循环

退出条件是,当用户没有按“退出”时,您可以使退出等于一个值

于 2013-11-13T00:39:52.560 回答