Java 如何设置<;ArrayList>;作为main()中的构造函数?
我对答案做了很多研究,并在我的代码中实现了它 我发现了错误Java 如何设置<;ArrayList>;作为main()中的构造函数?,java,arraylist,Java,Arraylist,我对答案做了很多研究,并在我的代码中实现了它 我发现了错误 必填项:字符串 找到:字符串,字符串,字符串,字符串,字符串,字符串,字符串 原因:实际参数列表和正式参数列表长度不同 我不明白为什么 基本上,我正在尝试制作一个名为easy的骰子,它是一个字符串 它有6条边,用字符串表示 填充方法只是用dice对象填充ArrayList 我的类Dice中有一个构造函数,我用它来设置骰子的名称和边,这就是错误发生的地方 我尝试了在网上找到的不同语法,将长度设置为5和6,但没有任何效果 import j
必填项:字符串
找到:字符串,字符串,字符串,字符串,字符串,字符串,字符串
原因:实际参数列表和正式参数列表长度不同
我不明白为什么
基本上,我正在尝试制作一个名为easy的骰子,它是一个字符串
它有6条边,用字符串表示
填充方法只是用dice对象填充ArrayList
我的类Dice中有一个构造函数,我用它来设置骰子的名称和边,这就是错误发生的地方
我尝试了在网上找到的不同语法,将长度设置为5和6,但没有任何效果
import java.util.*;
class diceGame{
// calling main method
public static void main (String[] args){
//Calling the method which asks user for how many players are
playing
//and also the players names.
Player [] arrayPlayers = createPlayersArray();
//Fills the cup with all 13 dices at the start of each turn
ArrayList<Dice> arrayDice = fillCup();
}
public static ArrayList<Dice> fillCup(){
//Player array to hold all the players
ArrayList<Dice> diceArray = new ArrayList<Dice>(13);
Dice easy = new Dice("Green:","Brain","Brain","Brain","Foot Print","Foot Print","Shotgun");
int numberOfEasyDice = 0;
for (int i = 0; i < 13; i++ ) {
if (numberOfEasyDice < 13) {
diceArray.add(easy);
numberOfEasyDice++;
}
};
return diceArray;
}
}
//////////////Dice class
import java.util.*;
class Dice{
public List<String> side;
public String name;
//This is the constructor I am using
public Dice (String n){
name = n;
side =new ArrayList<String>();
}
//Setter and Getter name
public String getName(){
return name;
}
public void setName(String n){
name = n;
}
public static int roll(){
Random r = new Random();
int rolledNumber = r.nextInt(6)+1;
System.out.println(rolledNumber);
return rolledNumber;
}
}
import java.util.*;
类骰子游戏{
//调用主方法
公共静态void main(字符串[]args){
//调用询问用户有多少玩家的方法
玩
//还有球员的名字。
播放器[]arrayPlayers=CreatePlayerArray();
//在每回合开始时,用所有13个骰子填满杯子
ArrayList arrayDice=fillCup();
}
公共静态数组列表fillCup(){
//玩家阵列可以容纳所有玩家
ArrayList diceArray=新的ArrayList(13);
骰子容易=新骰子(“绿色:”、“脑”、“脑”、“脑”、“脚印”、“脚印”、“猎枪”);
int numberOfEasyDice=0;
对于(int i=0;i<13;i++){
如果(数量easydice<13){
骰子数组。添加(简单);
numberOfEasyDice++;
}
};
返回数组;
}
}
//////////////骰子课
导入java.util.*;
类骰子{
公开名单方面;
公共字符串名称;
//这是我正在使用的构造函数
公共骰子(字符串n){
name=n;
side=newarraylist();
}
//Setter和Getter名称
公共字符串getName(){
返回名称;
}
公共void集合名(字符串n){
name=n;
}
公共静态int roll(){
随机r=新随机();
int rolled number=r.nextInt(6)+1;
System.out.println(rolledNumber);
返回滚动编号;
}
}
最终的结果是,每次使用骰子时,它都可以从ArrayList中取出 这里的错误是,您使用多个字符串调用Dice构造函数,即使该构造函数只接受一个字符串
// Errornous line
Dice easy = new Dice("Green:","Brain","Brain","Brain","Foot Print","Foot Print","Shotgun");
// compare the invocation to the constructor in Dice...
public Dice (String n) { ... }
因此,您必须替换初始化骰子实例的方式,如下所示:
// Initialize it this way instead
Dice easy = new Dice("Green");
但是,在我看来,你建立骰子类的方式,并不是你真正想要它工作的方式
我想澄清一下:
- 你说你想掷骰子
- 您希望骰子的每一面都有一个不同的字符串
- 但是:在骰子构造函数中只接受一个字符串(
)公共骰子(字符串n)
你的
Dice
构造函数被定义为publicdice(String n){
意思是当你构造一个Dice
对象时,它接受一个String
参数。然而,当你调用构造函数时,你正在做新的骰子(“绿色:,“大脑”,“大脑”,“脚印”,“猎枪”)
,传入7个字符串
参数作为输入。您需要传入一个字符串
作为输入,或者定义另一个可以接受7个字符串
输入的构造函数。哈哈,我真的很抱歉,这是我去年收到的一个任务,我很挣扎,所以我在第二年开始之前再次尝试了它。感谢e朝着正确的方向推进并提供帮助:)
class Dice {
public List<String> side;
public String[] names;
public Dice(String... names) {
this.names = names;
side = new ArrayList<String>();
}
// The rest of the class
}
Dice easy = new Dice("Green:","Brain","Brain","Brain","Foot Print","Foot Print","Shotgun");