不算数吧?

时间:2015-05-22 07:34:19

标签: c# loops for-loop switch-statement

我正在尝试使用switch语句来计算和收集不同变量的信息,但它最多只计算4个。

我知道我可以通过数组更轻松地做到这一点。

以下是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class WeekThreeLab
{
    static void Main()
    {
        String name, city;
        Double Drating;
        Double Judge1;
        Double Judge2;
        Double Judge3;
        Double Judge4;
        Double Judge5;
        Judge1 = 0;
        Judge2 = 0;
        Judge3 = 0;
        Judge4 = 0;
        Judge5 = 0;

        System.Console.Write("What is the drivers name: ");
        name = System.Console.ReadLine();
        System.Console.Write("What is the drivers city: ");
        city = System.Console.ReadLine();
        System.Console.Write("What is the degree of difficulty: ");
        Drating = Double.Parse(System.Console.ReadLine());
        while (Drating <= 0.99 || Drating >= 1.68)
        {
            Console.Write("Invalid! Try again! Degree of difficulty: ");
            Drating = Double.Parse(System.Console.ReadLine());
        }

        for (int count = 0; count <= 4; count++)
        {
            switch (count)
            {
                case 1:
                    System.Console.Write("Judge #{0} and his score? ", count);
                    Judge1 = Double.Parse(System.Console.ReadLine());
                    while (Judge1 < 0 || Judge1 > 10)
                    {
                        System.Console.Write("Invalid range! Reenter, Judge #{0}: ", count);
                        Judge1 = Double.Parse(System.Console.ReadLine());
                    }
                    break;
                case 2:
                    System.Console.Write("Judge #{0} and his score? ", count);
                    Judge2 = Double.Parse(System.Console.ReadLine());
                    while (Judge2 < 0 || Judge2 > 10)
                    {
                        System.Console.Write("Invalid range! Reenter, Judge #{0}: ", count);
                        Judge2 = Double.Parse(System.Console.ReadLine());
                    }
                    break;
                case 3:
                    System.Console.Write("Judge #{0} and his score? ", count);
                    Judge3 = Double.Parse(System.Console.ReadLine());
                    while (Judge3 < 0 || Judge3 > 10)
                    {
                        System.Console.Write("Invalid range! Reenter, Judge #{0}: ", count);
                        Judge2 = Double.Parse(System.Console.ReadLine());
                    }
                    break;
                case 4:
                    System.Console.Write("Judge #{0} and his score? ", count);
                    Judge4 = Double.Parse(System.Console.ReadLine());
                    while (Judge4 < 0 || Judge4 > 10)
                    {
                        System.Console.Write("Invalid range! Reenter, Judge #{0}: ", count);
                        Judge4 = Double.Parse(System.Console.ReadLine());
                    }
                    break;
                case 5:
                    System.Console.Write("Judge #{0} and his score? ", count);
                    Judge1 = Double.Parse(System.Console.ReadLine());
                    while (Judge5 < 0 || Judge5 > 10)
                    {
                        System.Console.Write("Invalid range! Reenter, Judge #{0}: ", count);
                        Judge5 = Double.Parse(System.Console.ReadLine());
                    }
                    break;
            }
        }
        System.Console.WriteLine(Judge1);
        System.Console.WriteLine(Judge2);
        System.Console.WriteLine(Judge3);
        System.Console.WriteLine(Judge4);
        System.Console.WriteLine(Judge5);
        System.Console.WriteLine(" ");
        System.Console.WriteLine("Your diver: {0} ");
        System.Console.WriteLine("From the city: {0} ");
        System.Console.WriteLine("Had a final score of: {0} ");
        System.Console.ReadKey();

    }
}

2 个答案:

答案 0 :(得分:3)

循环从0到4计数,但switch检查值1到5.要包括所有5个裁判,请将for循环更改为:

for (int count = 1; count <= 5; count++)

答案 1 :(得分:0)

更改为

this => for (int count = 0; count <= 5; count++){..}

并且在第五种情况下,您要设置judge1变量:

Judge1 = Double.Parse(System.Console.ReadLine());

将其更改为

Judge5 = Double.Parse(System.Console.ReadLine());