第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

為什么 eclipse 說我的方法沒有返回有效的結果?

為什么 eclipse 說我的方法沒有返回有效的結果?

Qyouu 2021-10-27 10:18:44
一段時間以來,我一直在用 Java 為數(shù)獨游戲編寫此代碼,但我不知道有什么問題,也許是“if”或 de“For”,但 IDE 說我的方法不返回布爾值類型。// check if the number has already been used in the columnsprivate boolean checkColumns(int x, int y, int value) {    for (int j = 0; j < 9; j++) {        if (this.gridPlayer[j][y].getValue() == value) return false;        else return true;    }    }// Check if the number has already been used in the linesprivate boolean checkLines(int x, int y, int value) {    for (int i = 0; i <= 9; i++) {        if (this.gridPlayer[x][i].getValue() == value) return false;         else return true;    }    }// Check if the number has already been used and the subGridprivate boolean checkSubGrid(int x, int y) {    for (int i = 0; i <= 9; i++) {        for (int j = 0; j <= 9; j++) {            if (this.gridPlayer[x][y].getValueOfSubGrid(x, y) == this.gridPlayer[i][j].getValueOfSubGrid(i, j)) {                if (this.gridPlayer[x][y].getValue() == this.gridPlayer[i][j].getValue()) {                    return false;                } else {                    return true;                }            } else if (this.gridPlayer[x][y].getValueOfSubGrid(x, y) != this.gridPlayer[i][j].getValueOfSubGrid(i,                    j)) {                return true;            }        }    }}
查看完整描述

2 回答

?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

歡迎,

在您的checkSubGrid()方法中,如果運行時未在最后一個輸入,您需要返回一個值else if: else if (this.gridPlayer[x][y]...) {


如果方法不是void,則需要返回。


 if(a > 1) {

   return a;

 } else {

   return b;

 }

在上面的這種情況下,我們有一個if - else語句,該方法將始終返回 true 或 false(或有異常)。


 if(a > 1) {

   return a;

 } else if(a == 0) {

   return b;

 }

另一方面,該方法可以或不能進入第二個if,他們沒有回報。您不能確保編譯器會返回。


您可以通過放置默認返回或放置 else 語句來解決此問題。


 if(a > 1) {

   return a;

 } else if(a == 0) {

   return b;

 } else {

   return null;

 }

或者


 if(a > 1) {

   return a;

 } else if(a == 0) {

   return b;

 }

 return null;


查看完整回答
反對 回復 2021-10-27
?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

編譯器假設它不是 100% 確定將調用“for”循環(huán)中的 return 語句,因此它看到的路徑是您的方法不返回任何值,即使它們聲明它們返回。


您需要在循環(huán)之外有一些返回值,即使您確定這永遠不會發(fā)生,即


private boolean checkLines(int x, int y, int value) {

  for (int i = 0; i <= 9; i++) {

    if (this.gridPlayer[x][i].getValue() == value) return false;

     else return true;

  }

 return false; //even if you think it will never be run it is necessary 

}


查看完整回答
反對 回復 2021-10-27
  • 2 回答
  • 0 關注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

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

公眾號

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