我创建了两个类,一个叫做“ Bucket ”,另一个叫做“ Player ”。
在“Bucket”中,我有一个构造函数方法,它创建一个包含三个值-1,1和1的虚拟存储桶(即小数组)。同时,我创建了一个获取存储桶数组的方法。
在“Player”类中,我创建了一个使用更大数组的方法(我已经调用了这个更大的数组“ArrayOfBuckets”),它使用循环在每个索引值存储一个新存储桶,直到某一点(当I> NumberSticks)。 但是,当我尝试设置时
ArrayofBuckets[i] = bucketInstance.getBucket();
我从Eclipse得到一个错误,说"Type mismatch: cannot convert from Bucket to int" 。 我花了一个小时试图解决这个问题,但无济于事。 任何帮助都会非常好。 非常感谢,这里是使用的所有代码:
玩家类:
import java.util.Scanner; public class Player { Scanner scan = new Scanner(System.in); public String name; private int pType; private int[] ArrayOfBuckets; public Player(String tempName) { this.name = tempName; //this. is unneccessary } public void ArrayOfBuckets(int NumberSticks) //this is a constructor method and creates the arrays that contains a { ArrayOfBuckets = new int[NumberSticks]; int i = 0; while(i<NumberSticks) { Bucket bucketInstance = new Bucket(); ArrayOfBuckets[i] = bucketInstance.getBucket();//new Bucket(); //ADD THIS i++; } }和水桶类:
import java.util.Random; public class Bucket { // private int[][] largeArray = null; //WTF DO I DO HERE private int AIChoiceStick; private int[] bucket; private Random random = new Random(); private int CurrentScore[] = new int[51]; //at max, if 100 sticks are initially chosen, then each player takes at max 50 sticks, private int h = 0; //^so why not have one more in case public Bucket() { bucket = new int[3]; bucket[0] = 1; bucket[1] = 1; bucket[2] = 1; public int[] getBucket() { return bucket; }I have created two classes, one called "Bucket", and the other called "Player".
In "Bucket", I have a constructor method that creates a virtual bucket (i.e. small array) that contains three values - 1, 1, and 1. As well, I created a method to get the bucket array.
In the "Player" class, I have created a method that uses a larger array (I have called this larger array "ArrayOfBuckets"), which uses a loop to store a new bucket at each index value, up until a certain point (when i>NumberSticks). However, when I try to set
ArrayofBuckets[i] = bucketInstance.getBucket();
I get an error from Eclipse, saying that "Type mismatch: cannot convert from Bucket to int". I have spent an hour trying to solve this, to no avail. Any help would be really nice. Thanks a lot, and here is all the code that is used:
The Player Class:
import java.util.Scanner; public class Player { Scanner scan = new Scanner(System.in); public String name; private int pType; private int[] ArrayOfBuckets; public Player(String tempName) { this.name = tempName; //this. is unneccessary } public void ArrayOfBuckets(int NumberSticks) //this is a constructor method and creates the arrays that contains a { ArrayOfBuckets = new int[NumberSticks]; int i = 0; while(i<NumberSticks) { Bucket bucketInstance = new Bucket(); ArrayOfBuckets[i] = bucketInstance.getBucket();//new Bucket(); //ADD THIS i++; } }and the Bucket Class:
import java.util.Random; public class Bucket { // private int[][] largeArray = null; //WTF DO I DO HERE private int AIChoiceStick; private int[] bucket; private Random random = new Random(); private int CurrentScore[] = new int[51]; //at max, if 100 sticks are initially chosen, then each player takes at max 50 sticks, private int h = 0; //^so why not have one more in case public Bucket() { bucket = new int[3]; bucket[0] = 1; bucket[1] = 1; bucket[2] = 1; public int[] getBucket() { return bucket; }最满意答案
你说的是一个二维数组。
private int[][] ArrayOfBuckets;您的ArrayOfBuckets(根据命名约定应该只是桶 )是一个数组数组,因此它有两组方括号。
然后初始化:
ArrayOfBuckets = new int[NumberSticks][3];顺便说一句,如果你想在一个数组中有50个元素的空间,那么初始化new int [50]所以它将有索引0到49。
You're speaking about a two-dimensional array.
private int[][] ArrayOfBuckets;Your ArrayOfBuckets (should be just buckets according to naming conventions) is an array of arrays, so it gets two sets of square brackets.
Then the initialization:
ArrayOfBuckets = new int[NumberSticks][3];By the way, if you want to have room for 50 elements in an array, then initialize it new int[50] so it will have indexes 0 through 49.
更多推荐
发布评论