亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當前上下文中不存在 C# 實例

當前上下文中不存在 C# 實例

C#
慕森王 2021-07-16 14:00:31
   Console.WriteLine("Are you a boy or a girl?");        string sex = Console.ReadLine();        Console.WriteLine(sex);        while ((sex != ("boy")) && (sex != ("girl")))        {            Console.WriteLine("That is not a valid sex. Please answer the question again.");            sex = Console.ReadLine();        }        if (sex == "boy")        {            Console.WriteLine("You are a boy");            Boy real_sex = new Boy            {                Firstname = "George",                Secondname = "Smith"            };        }        else if (sex == "girl")        {            Console.WriteLine("You are a girl");            Girl real_sex = new Girl            {                Firstname = "Charlotte",                Secondname = "Smith"            };        }        real_sex.Characteristics()         我是 C# 新手,所以這可能很容易,但我試圖制作一個程序,詢問用戶的性別,然后根據“男孩”或“女孩”的答案為班級創建一個實例。我已經在“Boy”和“Girl”類中創建了方法“Characteristics”。然而,問題出現在最后一行“real_sex.Characteristics()”,因為“real_sex”在“當前上下文”中不存在。顯然,要使用常規變量克服這個問題,您需要在 if 語句之前聲明變量,但在實例中它的行為似乎有所不同。任何人都可以幫忙嗎?謝謝你。
查看完整描述

2 回答

?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

這是一個范圍界定問題。當您在代碼塊內定義變量時,它不會存在于該代碼塊之外。例如:


int a = 2;

{

    int b = 3;

}

Console.WriteLine("A : " + a.ToString());

Console.WriteLine("B : " + b.ToString());

會打印 A 很好,但會在嘗試打印 B 時拋出錯誤,因為 B 是在打印語句之前結束的代碼塊中定義的。


解決方案是在與您需要的代碼塊相同(或更高)的代碼塊中定義您需要的變量。比較:


int a = 2;

int b = 0;

{

    b = 3;

}

Console.WriteLine("A : " + a.ToString());

Console.WriteLine("B : " + b.ToString());

這會正常工作,現在打印 A : 2 和 B : 3。


所以,改變


        if (sex == "boy")

        {

            Console.WriteLine("You are a boy");

            Boy real_sex = new Boy

            {

                Firstname = "George",

                Secondname = "Smith"

            };

        }

        else if (sex == "girl")

        {

            Console.WriteLine("You are a girl");

            Girl real_sex = new Girl

            {

                Firstname = "Charlotte",

                Secondname = "Smith"

            };

        }


        real_sex.Characteristics()    


        Sex real_sex = null;

        if (sex == "boy")

        {

            Console.WriteLine("You are a boy");

            real_sex = new Boy

            {

                Firstname = "George",

                Secondname = "Smith"

            };

        }

        else if (sex == "girl")

        {

            Console.WriteLine("You are a girl");

            real_sex = new Girl

            {

                Firstname = "Charlotte",

                Secondname = "Smith"

            };

        }


        real_sex.Characteristics()    

當然,您將需要一個名為“Sex”的父類,男孩和女孩從該類派生出來,以便您可以將 real_sex 設置為男孩或女孩。


查看完整回答
反對 回復 2021-07-18
?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

You have escope problem.

You need to declara the variable outside the if statement.  


If (){


Code and variables that only exist here can only run here 

//This method have to run here


Real_Sex.Characteristics();

Have to run here

}

Else {

Same here...

}

Or you can make a dynamic variable outside the scope




Console.WriteLine("Are you a boy or a girl?"); string sex = Console.ReadLine();

            dynamic real_sex;

            Console.WriteLine(sex); 

        while ((sex != ("boy")) && (sex != ("girl"))) 

    { 

    Console.WriteLine("That is not a valid sex. Please answer the question again."); 

        sex = Console.ReadLine(); }

         if (sex == "boy") {

         Console.WriteLine("You are a boy");

          real_sex = new Boy 

        { Firstname = "George", 

    Secondname = "Smith" }; 

    } 

        else if(sex == "girl") 

    { 

        Console.WriteLine("You are a girl"); 

             real_sex = new Girl

         { Firstname = "Charlotte", 

        Secondname = "Smith" }; 

    }

         real_sex.Characteristics();


查看完整回答
反對 回復 2021-07-18
  • 2 回答
  • 0 關注
  • 493 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號