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

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

數(shù)組中玩家的位置

數(shù)組中玩家的位置

湖上湖 2023-12-13 14:26:49
為什么我的保安(“S”)與唐納德(“D”)處于相同的位置。地圖應(yīng)該像這樣打印出來[D----][- - - - -][- - - - -][- - S - -][- - P - -]但它卻像這樣顯示[S----][- - - - -][- - - - -][- - - - -][- - P - -]public class Main {    public static void main(String[] args) {        Map m = new Map();        Player p = new Player();        Donald d = new Donald();        Security s = new Security();while(true) {            m.updateMap(p.row, p.col, p.character);            m.printMap();            m.updateMap(p.row, p.col, '-');            m.updateMap(d.row, d.col, d.character);            m.updateMap(s.row, s.col, s.character);            p.move();        }    }}public class Map {    char map[][];    Map() {        map = new char[5][5];        for(int i = 0; i<5; i++) {            for(int j = 0; j<5; j++) {                map[i][j] = '-';            }        }    }    void updateMap(int row, int col, char data) {        map[row][col] = data;    }    //prints map on the screen.     void printMap() {        for(int i = 0; i<5; i++) {            for (int j = 0; j<5; j++) {                System.out.print(map[i][j] + " ");            }            System.out.println();        }    }}public abstract class Position {    int row;    int col;    char character;    abstract void move();}public class Donald extends Position {    //Doanld Trump's Position on the Array is [0,0]    Donald() {        int row = 0;        int col = 0;        character = 'D';    }    void move() {    }}正如您在這里看到的,我將安全位置設(shè)置為 [3,2],但由于某種原因,它沒有將其識別為 [3,2],并將安全位置設(shè)置為 Donald 坐的 [0,0]。public class Security extends Position {    Security() {        int row = 3;        int col = 2;        character = 'S';    }    void move() {    }}
查看完整描述

1 回答

?
倚天杖

TA貢獻1828條經(jīng)驗 獲得超3個贊

該類Security繼承了屬性row和colfrom Position,但在構(gòu)造函數(shù)中,您正在執(zhí)行以下操作:


Security() {

    int row = 3;          //you are basically creating a new variable called row

    int col = 2;          //which is NOT the attribute (that is this.row)

    character = 'S';

}

在構(gòu)造函數(shù)之后,Security對象保持s.row等于s.col0。


你應(yīng)該做


Security() {

    this.row = 3;          //you can also do        row = 3;

    this.col = 2;          //and the compiler will understand

    this.character = 'S';

}

你在 中犯了同樣的錯誤Donald:你告訴Donald要在位置 (0,0) 但然后你告訴Security要在位置 (0,0),這就是為什么Security出現(xiàn)但Donald沒有出現(xiàn),他被覆蓋了Security。


Player正如您所設(shè)置的,它位于第 4 行第 2 列。


查看完整回答
反對 回復(fù) 2023-12-13
  • 1 回答
  • 0 關(guān)注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號