我想应该是你看视频没留心,没搞清楚bool类型的变量flag的作用,flag是用来限制Console.WriteLine("5个人的平均¨年龄为:{0}",average );语句- int age = 0;
- int sum = 0;
- int i = 0;
- double average;
- bool flag = false;
- while (i < 5)
- {
- try
- {
- Console.WriteLine("请输第{0}个人的年龄:", i + 1);
- age = Convert.ToInt32(Console.ReadLine());
- if (age < 0)
- {
- Console.WriteLine("年龄必须大于0");
- flag = false;
- break;
- }
- else if (age > 100)
- {
- Console.WriteLine("年龄必须在1—100之间");
- flag = false;
- break;
- }
- else
- {
- sum = sum + age;
- i++;
- flag = true;
- }
- }
- catch
- {
- Console.WriteLine("请输入1~100的数字!");
- flag = false;
- break;
- }
- }
- average = sum / 5.0;
- if (flag)
- {
- Console.WriteLine("5个人的平均¨年龄为:{0}", average);
- }
- Console.ReadKey();
复制代码 的, |