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

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么我這個輸出的是:我今年1歲了和我今年2歲了?


using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace Youeryuan

{

? ? class Program

? ? {

? ? ? ? static void Main(string[] args)

? ? ? ? {

? ? ? ? ? ? Child c1 = new Child("曉強", 3);

? ? ? ? ? ? Growth(c1);

? ? ? ? ? ? Growth(c1.Age);

? ? ? ? ? ? Console.ReadLine(); ? ? ? ? ??


? ? ? ? }

? ? ? ?

? ? ? ? static void Growth(Child chlid)

? ? ? ? {

? ? ? ? ? ? chlid.Age++;

? ? ? ? ? ? Console.WriteLine("我今年"+chlid.Age+"歲了1!");

? ? ? ? ? ?// Console.WriteLine("我又長大了一歲!");

? ? ? ? ? ??

? ? ? ? }

? ? ? ? static void Growth (int age)

? ? ? ? {

? ? ? ? ? ? age++;

? ? ? ? ? ? Console.WriteLine("我今年{0}歲了2!", age);

? ? ? ? }

? ? ? ?

? ? }

}


正在回答

5 回答

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace ceshi1

{

? ? class Program

? ? {

? ? ? ? class Child

? ? ? ? {

? ? ? ? ? ? private string name;


? ? ? ? ? ? public string Name

? ? ? ? ? ? {

? ? ? ? ? ? ? ? get { return name; }

? ? ? ? ? ? ? ? set { name = value; }

? ? ? ? ? ? }

? ? ? ? ? ? private int age;


? ? ? ? ? ? public int Age

? ? ? ? ? ? {

? ? ? ? ? ? ? ? get { return age; }

? ? ? ? ? ? ? ? set { age = value; }

? ? ? ? ? ? }

? ? ? ? ? ? public Child(string name, int age)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? this.name = name;

? ? ? ? ? ? ? ? this.age = age;

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? static void Main(string[] args)

? ? ? ? {


? ? ? ? ? ? Child c1 = new Child("曉強", 3);


? ? ? ? ? ? Growth(c1);


? ? ? ? ? ? Growth(c1.Age);


? ? ? ? ? ? Console.ReadLine();




? ? ? ? }




? ? ? ? static void Growth(Child chlid)

? ? ? ? {


? ? ? ? ? ? chlid.Age++;


? ? ? ? ? ? Console.WriteLine("我今年" + chlid.Age + "歲了1!");


? ? ? ? ? ? // Console.WriteLine("我又長大了一歲!");




? ? ? ? }


? ? ? ? static void Growth(int age)

? ? ? ? {


? ? ? ? ? ? age++;


? ? ? ? ? ? Console.WriteLine("我今年{0}歲了2!", age);


? ? ? ? }


? ? }

}

應該是構造方法沒有給age賦值的錯誤,main方法中實例化Child類對象c1時傳的參數3到構造方法為age賦值的代碼錯誤,建議檢查構造方法,如果沒有給age賦值,則默認int型數據初始為0,也就會出現輸出為??

我今年1歲了!

我今年2歲了!

0 回復 有任何疑惑可以回復我~

你的Main方法和兩個Growth方法平級的,也就是說,程序開始時直接進入 Growth(Child child) , Child.Age默認取0,child.Age++ 后得1,這里輸出了“我今年1歲了1!”。接下來上面的 Growth(c1.Age) 讀取到的值為1,再傳到Growth(int age)中,age讀取到的值為1,age++ 得到2,故,這里輸出“我今年2歲了2!”?

望指點

0 回復 有任何疑惑可以回復我~

應該是你的構造方法中沒有給年齡Age初始化,導致c1對象Age取默認值0

2 回復 有任何疑惑可以回復我~

? static void Growth (int age)

? ? ? ? {

? ? ? ? ? ? age++;

? ? ? ? ? ? Console.WriteLine("我今年{0}歲了2!", age);

? ? ? ? }

上面代碼你輸出的age是這個方法內的值,在這個方法里age的初始值是零,你的代碼age++,輸出的結果自然就1啦

0 回復 有任何疑惑可以回復我~
  1. 首先你需要把兩個輸出語句中的child.Age和age都改成c1.Age,因為是輸出對象的年齡屬性必須用?對象.屬性?這種方式輸出.

  2. 其次你需要把兩條輸出語句放入主方法也就是Main函數內部執行,因為對象c1是在Main函數內部實例化的。

  3. 最后加油加油加油

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

為什么我這個輸出的是:我今年1歲了和我今年2歲了?

我要回答 關注問題
微信客服

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

幫助反饋 APP下載

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

公眾號

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