常量的使用

namespace 示例2
{
    class Program
    {
        static void Main(string[] args)
        {
            //这段代码是想今天的星期数加1后输出,关注输出结果是否正确
            const int dayMax = 7; //每周的天数
            int today = 1; //今天的星期数
            Console.WriteLine("一周有几天");
            Console.WriteLine(dayMax);
            Console.WriteLine("明天是周");
            // dayMax = dayMax + 1; 常量只能在定义时修改,强制修改会报错,这样就保证了 dayMax不会被误操作
            today = today + 1;
            Console.WriteLine(today);
        }
    }
}

//由于程序员粗心 把"today=today+1" 写成 “dayMax=dayMax+1”
输出结果为8,与现实不符的情况,这种情况可以用常量来解决 // * 什么是常量? // * 常量:常量就是在程序运行过程中保持不变的值。
// * 例如以上示例代码 dayMax表示一周的天数就可以定义为常量。 // * 常量语法:const 数据类型常量名称 = 值
例如:publi const int datMax = 7 // * 常量命名规范: // *
具有一定的实际意义。最好以大写字母来命名,中间根据意义的连续性使用下划线做连接,最好注明注释。 // *
常量名称最好不要超过25个字符,否则可续性差。
常量的使用c1

相关文章:

  • 2022-02-28
  • 2021-06-26
  • 2022-12-23
  • 2021-08-08
  • 2022-12-23
  • 2021-05-23
  • 2021-11-06
  • 2022-12-23
猜你喜欢
  • 2021-12-23
  • 2022-12-23
  • 2021-06-26
  • 2021-10-11
  • 2021-12-23
  • 2021-05-22
  • 2022-01-05
相关资源
相似解决方案