关于C#:如何将”Monday”转换为整数1,将”Tuesday”转换为2,等等,一直转换到星期五?


How do i convert “Monday” to the integer 1 and “Tuesday” to 2 etc all the way to friday?

本问题已经有最佳答案,请猛点这里访问。

我让一个用户输入一天(星期一)和一周的数字,以便在数组中搜索值。但是,要获得数组输出,我需要行和列的编号。行数可以从用户输入中获得,1,2等。但是,他们输入星期一,星期二等的部分比较复杂。是否有一种方法可以将星期一、星期二等转换为整数(1、2),以便使用它查找数组元素?

我试过这个方法,但没用。将值转换为int显然不起作用,因为"monday"不是一个数字。有没有其他方法可以做到这一点?

1
2
3
4
int Value = Convert.ToInt32(value);

DateTime ClockInfoFromSystem = DateTime.Now;
Value = (int)ClockInfoFromSystem.DayOfWeek;

如果需要,这是代码的其余部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
private void AddToArray()
{
    txtOutput.Text ="Filling the array with user input..." +"



"
;

    String value;
    int num;

    for (int week = 0; week < productsArray.GetLength(0); week++)
    {
        for (int day = 0; day < productsArray.GetLength(1); day++)
        {
            value = Microsoft.VisualBasic.Interaction.InputBox("Enter Value for Day" + day +" of Week" + week,"Enter Value");
            try
            {
                while (!(int.TryParse(value, out num)))
                {
                    MessageBox.Show("Not a valid number, try again.");
                    value = Microsoft.VisualBasic.Interaction.InputBox("Enter a Number","Enter Number");

                }
            }
            catch (Exception)
            {
                MessageBox.Show("Value entered is not in a valid format");
            }

            productsArray[week, day] += int.Parse(value);
        }
    }
    txtOutput.Text +="The product allocation is as follows:" +"



"
;
}

private void Array()
{
    txtOutput.Text +="\tMon\tTue\tWed\tThu\tFri

"
;
    for (int week = 0; week < productsArray.GetLength(0); week++)
    {
        txtOutput.Text +="Week" + (week + 1) +"\t";
        for (int day = 0; day < productsArray.GetLength(0); day++)
        {
            txtOutput.Text += productsArray[week, day] +"\t";
        }
        txtOutput.Text +="

"
;
    }

    txtOutput.Text +="

"
+"Retrieve products completed on a specific day." +"

"
;
    String value = Microsoft.VisualBasic.Interaction.InputBox("Enter Day","Enter Day","Monday");

    int num;

    String value2 = Microsoft.VisualBasic.Interaction.InputBox("Enter Week","Enter Week");
    try
    {
        while (!(int.TryParse(value2, out num)))
        {
            MessageBox.Show("Not a valid number, try again.");
            value2 = Microsoft.VisualBasic.Interaction.InputBox("Enter Week","Enter Week");
        }
    }
    catch (Exception)
    {
        MessageBox.Show("Value entered is not in a valid format");
    }
    int Value2 = Convert.ToInt32(value2);
    int Value = Convert.ToInt32(value);

    DateTime ClockInfoFromSystem = DateTime.Now;
    Value = (int)ClockInfoFromSystem.DayOfWeek;

    var output = productsArray[Value2, Value];

    txtOutput.Text +="Products completed on that day are:" + output;

}


我建议编写一种方法,例如使用简单的switch语句或if...else if...else链实现的dayToInt(String day)