-3

所以,我对整个 C# 语言有点陌生,有人要求我们将他们的简单 if else 转换为 switch。这就是我所知道的。这是对的吗?如果不是,你能告诉我问题出在哪里吗?还是缺少什​​么?这里唯一的问题是开关盒的输出没有出现。

    static void Main(string[] args)
    {

        Console.Write("Enter Name: ");
        string studname = Console.ReadLine();
        Console.Write("Enter Age: ");
        string age = Console.ReadLine();
        Console.Write("Enter Year Level: ");
        int level = int.Parse(Console.ReadLine());
        Console.Write("Enter Course: ");
        string course = Console.ReadLine();
        Console.Write("Enter Score 1: ");
        int num1 = int.Parse(Console.ReadLine());
        Console.Write("Enter Score 2: ");
        int num2 = int.Parse(Console.ReadLine());
        Console.Write("Enter Score 3: ");
        int num3 = int.Parse(Console.ReadLine());
        int total = num1 + num2 + num3;
        int ave = (num1 + num2 + num3) / 3;
        int rate = ave;




        Console.WriteLine("");
        Console.WriteLine("");
        Console.WriteLine("");
        Console.Write("Student Name: " + studname);
        Console.WriteLine("");
        Console.Write("Student Age: " + age);
        Console.WriteLine("");
        Console.Write("Student Year Level: " + level);
        Console.WriteLine("");
        Console.Write("Student Course: " + course);
        Console.WriteLine("");
        Console.Write("Score 1: " + num1);
        Console.WriteLine("");
        Console.Write("Score 2: " + num2);
        Console.WriteLine("");
        Console.Write("Score 3: " + num3);
        Console.WriteLine("");
        Console.Write("Total_Score= " + total);
        Console.WriteLine("");
        Console.Write("Total_Average= " + ave);


        switch (rate)
        {
            case 1:
                if (rate < 100 && rate > 90)

                Console.Write("Rate = 1.00");
                Console.WriteLine("");
                Console.Write("Hi! " + studname + "you Passed");
                Console.WriteLine("");
               break;


            case 2:
                if (rate < 89 && rate > 85)

                    Console.Write("Rate = 1.00");
                Console.WriteLine("");
                Console.Write("Hi! " + studname + "you Passed");
                Console.WriteLine("");
                break;

            case 3:
                if (rate < 84 && rate > 80)

                    Console.Write("Rate = 1.00");
                Console.WriteLine("");
                Console.Write("Hi! " + studname + "you Passed");
                Console.WriteLine("");
                break;

            case 4:
                if (rate < 79 && rate > 75)

                    Console.Write("Rate = 1.00");
                Console.WriteLine("");
                Console.Write("Hi! " + studname + "you Passed");
                Console.WriteLine("");
                break;

            case 5:
                if (rate < 75)

                    Console.Write("Rate = 5.00");
                Console.WriteLine("");
                Console.Write("Hi! " + studname + "you Failed");
                Console.WriteLine("");
                break;


        }





        Console.ReadLine();
    }
4

2 回答 2

1

您误解了switch-block 的作用。如果您正在测试的值仅采用几个设定值之一,则使用它。

尝试完全摆脱switch-block 并重新编写它以使用一系列if/ else-blocks。另请注意,您需要处理边缘情况,例如rate = 90;您还需要检查rate >= 90以捕获这种情况:

if (rate < 100 && rate >= 90)
{
    Console.Write("Rate = 1.00");
    Console.WriteLine("");
    Console.Write("Hi! " + studname + "you Passed");
    Console.WriteLine("");
}
else if (rate < 89 && rate >= 85)
{
    Console.Write("Rate = 1.00");
    Console.WriteLine("");
    Console.Write("Hi! " + studname + "you Passed");
    Console.WriteLine("");
}
else ...

或者,如果您真的想在每种情况下都做同样的事情,除非rate < 75,您可以将其简化为:

if (rate >= 75)
{
    Console.Write("Rate = 1.00");
    Console.WriteLine("");
    Console.Write("Hi! " + studname + "you Passed");
    Console.WriteLine("");
}
else
{
     Console.Write("Rate = 5.00");
     Console.WriteLine("");
     Console.Write("Hi! " + studname + "you Failed");
     Console.WriteLine("");
}

甚至只是这样的:

Console.Write("Rate = {0}", (rate >= 75) ? "1.00" : "5.00");
Console.WriteLine("");
Console.Write("Hi! {0} you {1}", studname, (rate >= 75) ? "Passed" : "Failed");
Console.WriteLine("");
于 2013-11-10T05:50:45.557 回答
0

您的 case 值不正确,switch 语句正在切换 case 值,因此在这种情况下,只有在 rate == 1 时才会调用 case 1,依此类推,因此您需要修改这些值才能拥有它输出。

于 2013-11-10T05:56:30.833 回答