为了账号安全,请及时绑定邮箱和手机立即绑定

为什么这样不对?QAQ

为什么这样不对?QAQ

C#
qq_守夜人_11 2017-12-02 16:59:41
这样不对using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _6._5{    class Program    {        static void Main(string[] args)        {            int  dt = DateTime.Now.Month ;            Console.WriteLine("您现在处于{0}月", dt);            string w1 ;             switch (dt)            {                case 12:                case 1:                case 2: w1 = "您现在处于冬季!"; break;                case 3:                case 4:                case 5: w1 = "您现在处于春季!"; break;                case 6 :                case 7 :                case 8: w1 = "您现在处于夏季!"; break;                case 9 :                 case 10 :                case 11: w1 = "您现在处于秋季"; break;             }            Console.WriteLine(w1);            Console.ReadLine();        }    }}但是这样就对了。。。using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _6._5{    class Program    {        static void Main(string[] args)        {            int  dt = DateTime.Now.Month ;            Console.WriteLine("您现在处于{0}月", dt);            string w1 ="";             switch (dt)            {                case 12:                case 1:                case 2: w1 = "您现在处于冬季!"; break;                case 3:                case 4:                case 5: w1 = "您现在处于春季!"; break;                case 6 :                case 7 :                case 8: w1 = "您现在处于夏季!"; break;                case 9 :                 case 10 :                case 11: w1 = "您现在处于秋季"; break;             }            Console.WriteLine(w1);            Console.ReadLine();        }    }}但是课本上明明就是第一种写法啊,我的报错,使用了未赋值的变量w1.。。。。。明明已经赋值。。。。QwQ
查看完整描述

2 回答

?
慕仔0944135

TA贡献6条经验 获得超0个赞

你那个switch没default,也就是说如果没在那个范围直接跳出来,然后Console.WriteLine(w1)的时候会有w1没赋值的情况,编译器不允许吧。

查看完整回答
反对 回复 2018-05-27
?
笔墨遥仙

TA贡献5条经验 获得超1个赞

局部变量,要使用就必须先赋值!

查看完整回答
反对 回复 2017-12-03
  • 2 回答
  • 0 关注
  • 1410 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信