0

我需要制作一个运行 3 次的循环和三个年龄段的开关,0-10 11-2021-65使用以下代码:

class Program
    {
        private static object N;
        static void Main(string[] args)
        {
            string answer;                                   
            {
              Console.WriteLine("please enter your name:");
              string name = Console.ReadLine();

              Console.WriteLine("please enter your surname:");
              string surname = Console.ReadLine();

              Console.WriteLine("please enter your age:");
              string age = Console.ReadLine();        

              Console.WriteLine("please enter your adress:");
              string adress = Console.ReadLine();

              Console.WriteLine("hallo,{0} {1},veel sucess met C#", name, surname);

              Console.WriteLine("zijn deze gegevens juist? J/N");
              answer = Console.ReadLine();
            }
            while(answer == "N");
        }
   }      

我尝试了一些东西来测试年龄,但我总是出错。另外,我不知道如何正确编写循环代码。

有人可以为这两个问题指出正确的方向吗?

对不起我的英语不好,我是荷兰人。

注意:在这不是家庭作业的答案之一下提到的 OP。

4

3 回答 3

1

Switch 语句对特定值而不是范围进行操作,因此您希望使用系列if语句。

使用计数器(在循环外声明并在每次执行时递增)来确定循环运行了多少次(或使用for循环)。

编辑

class Program
{
    private static object N;

    static void Main(string[] args)
    {
        string answer;
        int runCount= 0;

        do
        {
          ++runCount;
          Console.WriteLine("please enter your name:");
          string name = Console.ReadLine();

          Console.WriteLine("please enter your surname:");
          string surname = Console.ReadLine();

          Console.WriteLine("please enter your age:");
          int age = int.Parse(Console.ReadLine());

          if(age >= 0 && age <= 10)
          {
              Console.WriteLine("Child");
          }
          else if(age <= 20)
          {
              Console.WriteLine("Young adult");
          }
          else if(age <= 65)
          {
              Console.WriteLine("Adult");
          }

          Console.WriteLine("please enter your adress:");
          string adress = Console.ReadLine();

          Console.WriteLine("hallo,{0} {1},veel sucess met C#", name, surname);

          Console.WriteLine("zijn deze gegevens juist? J/N");
          answer = Console.ReadLine();
        }
        while(runCount < 3 && answer == "N");
    }
}
于 2012-10-19T07:50:52.647 回答
1

您可能想要以下内容:

var ageGroups = new [] { "0-10",  "11-20", "21-65"};

foreach(var ageGroup in ageGroups)
{
    // do your thing. This will loop three times, once for each age group
    // example of switch (but: code-smell, you probably want class hierarchy later)
    switch(ageGroup)
    {
        case "0-10":
         // do somethihg
         break;
    }
}

或者,正如 MrFox 所说,当您没有预定义的年龄组(但我在您的问题中没有看到)时:

for(var i = 0; i < 3; i++)
{
    // do your thing
    // example of if-statement for range (cannot do with switch):
    int convertedAge;
    if(int.TryParse(age, out convertedAge))
    {
        if(convertedAge >= 0 && convertedAge <= 10)
        {
             // do your thing
        }
    }
}
于 2012-10-19T07:54:26.997 回答
0

您的界面是多语言的,有些文本是用英语写的,有些是用荷兰语写的,下定决心或允许用户选择他的语言。

至于编程技能,你应该谷歌通用 C# 结构。这门课也很好。最后,作为一名程序员,无论您来自哪个国家,您都最好学习英语。

class Program
{
    static int age;
    static void Main(string[] args)
    {
        string answer;
        do
        {
            Console.WriteLine("please enter your name:");
            string name = Console.ReadLine();

            Console.WriteLine("please enter your surname:");
            string surname = Console.ReadLine();

            Console.WriteLine("please enter your age:");
            age = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("please enter your adress:");
            string adress = Console.ReadLine();

            Console.WriteLine("hallo,{0} {1},veel sucess met C#", name, surname);

            Console.WriteLine("zijn deze gegevens juist? J/N");
            answer = Console.ReadLine();
        }
        while (answer == "N");

        for (int i = 0; i < 3; i++)
        {
            if (age >= 0 && age <= 10)
            {
                // First age group.
            }
            if (age >= 11 && age <= 20)
            {
                // Second age group.
            }
            if (age >= 21 && age <= 65)
            {
                // Third age group.
            }
        }
    }
}

编辑:谢谢,现在可以使 while 循环正确。我不清楚 for 循环应该去哪里。

于 2012-10-19T08:03:36.357 回答