2013-12-10 29 views
-2

目標: 檢索另一個類中的Player類中定義的數組,以便對數組內容執行額外的操作;Java數組:我想創建兩個數組,每個數組保存3個int值

到目前爲止的代碼:

public class Players { 

//attributes 

private int [] Player1 = new int [3]; 
private int [] Player2 = new int [3]; 
private int round=Math.random(); 

//Constructor 

public Players(int [] p1, int [] p2){ 
[] player1 = [] p1 
[] player2 = [] p2 
} 

問題說明:沒有編制;

+3

我可能是錯的,但我有一種感覺,你沒有遵循OOP標準。您應該創建一個'Player'類,然後在main方法中爲player1/2實例化它的兩個實例。 – turbo

+0

可能的重複http://stackoverflow.com/questions/1200621/how-to-declare-an-array-in-java – Freak

回答

1

你必須分開參數低谷逗號

public Game(int[] p1, int[] p2) { 

這是不是甚至接近工作(注意缺少分號一樣):

[] player1 = [] p1 
[] player2 = [] p2 

你想設置實例字段等於給定參數,但不需要重新定義它們是數組。

而是使用此:

this.player1 = p1; 
this.player2 = p2; 

記住命名約定:字段以小寫(這麼player1代替Player1)開始。 Java區分大小寫,因此Player1player1不一樣。命名約定存在的原因之一。

最後:Math.random返回double。如果你將它投擲到int你將失去​​它的目的(它返回一個介於0和1之間的值,如果你將它轉換爲int,那麼你將失去這兩個邊界之間的所有東西)。相反,讓它這樣:

private double round = Math.random(); 
2

您有多個語法和類型不匹配的問題

1) Java是大小寫敏感的

public Players(int [] p1, int [] p2){ 
Player1 = p1; 
Player1 = p2; 
} 

所以

Player1 !=player1 

2)

[] player1 = [] p1 

是不是一個有效的Java語法

3)最後Math.random()回報double,將其更改爲

double random = Math.random(); 
0

有幾次失誤與你的程序。 檢查下面的一個:

public class Players { 

    //attributes 
    private int [] player1 = new int [3]; // use names with lowercase at beginning (convention) 
    private int [] player2 = new int [3]; 
    private double round = Math.random(); 

    //Constructor 
    public Players(int[] p1, int[] p2) { // you missed a comma and a parenthesis 
     player1 = p1; // no need to say again that they are arrays. add ';' at the end 
     player2 = p2; 
    } 

    // you could need to add these to access your private arrays from an other class 
    public int[] getPlayer1(){ 
     return player1; 
    } 

    public int[] getPlayer2(){ 
     return player2; 
    } 
} 

希望它可以幫助

0
  • 通過PLAYER1PLAYER1當心區分大小寫。
  • 添加一些分號
  • 除去一些括號用於設定數組變量
  • 的Math.random()方法返回一個雙,不是整數
  • 變量應與小寫字母
開始

像這樣:

public class Players { 

    //attributes 

    private int[] player1 = new int[3]; 
    private int[] player2 = new int[3]; 
    private double round = Math.random(); 

    //Constructor 

    public Players(int[] p1, int[] p2) { 
     player1 = p1; 
     player2 = p2; 
    } 
} 
1

根據我的評論,你可能想要考慮像這樣構建你的班級:

public class Player { 
    //attributes 
    private int[] roundValues; 
    private double round=Math.random(); //not sure what you are using this for 

    //Constructor 
    public Player(int[] roundValues){ 
     this.roundValues = roundValues; 
    } 

    // use this to get the roundValues array 
    public int[] getRoundValues() { 
     return roundValues; 
    } 

    // use this to change the roundValues array 
    public void setRoundValues(int[] roundValues) { 
     this.roundValues = roundValues; 
    } 

    public static void main(String[] args) { 
     int[] array1 = {1,2,3}; //just some dummy values 
     Player player1 = new Player(array1); 
     // do this to get the player's round values 
     int[] roundVals = player1.getRoundValues(); 
    } 
} 
+0

數組應該在數組中保存圓的值。然後我想從另一個類中調用該數組。 – user3061322

+0

@ user3061322好的,我會更新我的答案,這樣你就可以通過那裏的獲得者和制定者來做到這一點@trurbo – turbo

+0

@turbo。 –

相關問題