Java数组:我想创建两个数组,每个数组包含3个int值
目标: 检索另一个类中的类Player中定义的数组,对数组内容执行附加操作 到目前为止的代码:Java数组:我想创建两个数组,每个数组包含3个int值,java,arrays,oop,Java,Arrays,Oop,目标: 检索另一个类中的类Player中定义的数组,对数组内容执行附加操作 到目前为止的代码: 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 ){
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
}
问题说明:未编译 必须用逗号分隔参数
public Game(int[] p1, int[] p2) {
这甚至还不起作用(请注意没有分号):
您希望将实例字段设置为与给定参数相等,但无需重新定义它们是数组
相反,请使用以下命令:
this.player1 = p1;
this.player2 = p2;
请记住命名约定:字段以小写开头(因此player1
而不是player1
)。Java区分大小写,因此Player1
和Player1
不一样。这正是命名约定存在的原因之一
最后:Math.random
返回一个double
。如果将其转换为int
,则将失去其用途(它返回一个介于0和1之间的值,如果将其转换为int,则将失去这两个边界之间的所有内容)。取而代之的是:
private double round = Math.random();
您有多个语法以及类型不匹配问题 1)Java区分大小写
public Players(int [] p1, int [] p2 ){
Player1 = p1;
Player1 = p2;
}
所以
2)和
[] player1 = [] p1
不是有效的java语法
3)最后Math.random()
返回double
,将其更改为
double random = Math.random();
你的程序有几个错误。 请勾选以下选项:
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;
}
}
希望能有所帮助
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;
}
}
根据我的评论,你可能想考虑这样构造你的类:
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();
}
}
我可能错了,但我感觉你没有遵循OOP标准。您应该创建一个
Player
类,然后在一个main方法中为player1/2实例化它的两个实例。数组的可能副本应该包含数组中的舍入值。然后我想从另一个类调用数组。@user3061322好的,我将更新我的答案,以便您可以执行此操作that@turbo只是通过那里的接球手和二传手;
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;
}
}
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();
}
}