Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java数组:我想创建两个数组,每个数组包含3个int值_Java_Arrays_Oop - Fatal编程技术网

Java数组:我想创建两个数组,每个数组包含3个int值

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 ){

目标: 检索另一个类中的类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 ){
[] 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;
    }
}
希望能有所帮助
  • 通过player1player1注意区分大小写
  • 添加一些分号
  • 删除一些大括号以设置数组变量
  • Math.random()方法返回一个double,而不是整数
  • 变量应以小写字母开头
  • 像这样:

    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();
        }
    }