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

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

大佬們,這樣的話有問題嗎?

public class HelloWorld {

? ? ? // 定義靜態變量score1

? ? static int score1 = 86;

? ? // 定義靜態變量score2

static? ? ? ? ? int score2 = 92;?

?// 定義靜態方法sum,計算成績總分,并返回總分

public static? ? int sum() {?

? ? ? int d=score1+score2;

? ? ? return d;? ? ? }

public static void main(String[] args) {

? ? ? ? HelloWorld s=new HelloWorld();

? ? ? ? // 調用靜態方法sum并接收返回值

int allScore = s.sum();? ? ? ?

System.out.println("總分:" + allScore);

}

}

能夠運行成功,但是和老師的不一樣。

如果不行的話,為什么不行?


正在回答

4 回答

int d=score1+score2;

? ? ? return d;?

這樣是對的,但是他只要求要求一行代碼。

都行,一個意思,sum()方法就是屬于HelloWorld類中。

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

public class HelloWorld {

? ? ? // 定義靜態變量score1

? ? static int score1 = 86;

? ? // 定義靜態變量score2

static? ? ? ? ? int score2 = 92;?

?// 定義靜態方法sum,計算成績總分,并返回總分

public static? ? int sum() {?

? ? ? int d=score1+score2;

? ? ? return d;? ? ? }

public static void main(String[] args) {

? ? ? ? HelloWorld s=new HelloWorld();? ? ? 這一行為什么要這樣寫?不是只有靜態方法調用非靜態方法才需要創建對象嗎?

? ? ? ? // 調用靜態方法sum并接收返回值

int allScore = s.sum();? ? ? ?

System.out.println("總分:" + allScore);

}

}


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

第9行為什么不能直接用 sum = score1 +score2;而需要用到return呢?

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

慕斯卡3555197 提問者

sum是方法名啊…自己重新復習吧
2019-07-21 回復 有任何疑惑可以回復我~

https://img1.sycdn.imooc.com//5d29f6c60001696304150344.jpghttps://img1.sycdn.imooc.com//5d29f6eb0001168003890332.jpg14行這樣寫都行,為什么?哪樣更好?

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

舉報

0/150
提交
取消

大佬們,這樣的話有問題嗎?

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

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

幫助反饋 APP下載

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

公眾號

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