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

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

java關于調用其他類中的屬性

java關于調用其他類中的屬性

慕容3067478 2022-07-14 16:57:34
class GameLauncher{    public static void main(String[] args){        GuessGame Game1 = new GuessGame();        Game1.startGame(p1);        Game1.startGame(p2);        Game1.startGame(p3);    } }public class GuessGame {    public Player p1 = new Player();    public Player p2 = new Player();    public Player p3 = new Player();    void startGame(Player p){        Scanner scanner = new Scanner(System.in);        int n = scanner.nextInt();        p.guess(n);    }}class Player{    private int number;    Player(){        number = (int)Math.random();    }    void guess(int n){        if(number == n){            System.out.println("the correct answer");        }        System.out.println("the wrong answer");    }}我的代碼有一些錯誤:Game1.startGame(p1); Game1.startGame(p2); Game1.startGame(p3);編譯器說它找不到p1, p2, p3的符號,但我已經在GuessGame類中初始化了Player如何修復錯誤?對不起我的英語不好。
查看完整描述

3 回答

?
海綿寶寶撒

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

p1、p2 和 p3 在您的 GuessGame 類中聲明,因此您的 GameLauncher 方法看不到這些。您應該將這些變量設為全局變量,或者在 GameLauncher 中聲明它們,因為您的 GuessGame 不使用它們。


編輯代碼:


class GameLauncher{

public static void main(String[] args){

Player p1 = new Player();

Player p2 = new Player();

Player p3 = new Player();

    GuessGame Game1 = new GuessGame();

    Game1.startGame(p1);

    Game1.startGame(p2);

    Game1.startGame(p3);

    } 

}

public class GuessGame {


void startGame(Player p){


Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();

p.guess(n);


}

}


class Player{

     private int number;

        Player(){

         number = (int)Math.random();

    }

    void guess(int n){

        if(number == n){

        System.out.println("the correct answer");}

        System.out.println("the wrong answer");

    }


}


查看完整回答
反對 回復 2022-07-14
?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

如果你想運行代碼:

  • 將您的 main() 方法放在公共類中

  • 利用 。用于訪問 GuessGame 屬性的運算符。

如果您希望您的代碼更好:

  • 更改 GuessGame 中的訪問修飾符:public -> private 并使用 getter/setter 進行訪問。

  • 對guess() 方法使用if/else 表達式。


公共類 GameLauncher {

    public static void main(String[] args) {


        GuessGame game1 = new GuessGame();

        Player p1 = new Player();

        Player p2 = new Player();

        Player p3 = new Player();


        game1.startGame(p1);

        game1.startGame(p2);

        game1.startGame(p3);

    }

}


class GuessGame {

    void startGame(Player p) {

        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();

        p.guess(n);


    }

}


class Player {

    private int number;


    Player() {

        number = (int) Math.random();

    }


    void guess(int n) {

        if (number == n) {

            System.out.println("the correct answer");

        } else {

            System.out.println("the wrong answer");

        }

    }

}


查看完整回答
反對 回復 2022-07-14
?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

您必須使用Game1.startGame(Game1.p1)sincep1GuessGameand not的實例字段GameLauncher。



查看完整回答
反對 回復 2022-07-14
  • 3 回答
  • 0 關注
  • 196 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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