我需要在我的Java类的最后学校作业中实现一个接口。被覆盖的界面应该打印一条短信,描述一名足球运动员如何庆祝在NFL被选中,这样的话:
System.out.println("name dances to celebrate his draft");
然而,当我将其放入允许用户将玩家添加到其名单中的方法时,我的界面不会打印消息。以下是程序中重写的界面:
//Overridden celebrate method
@Override
public void celebrate(int i, int place){
int randomInteger;
if (place == 0) //Player is offensive
{
randomInteger = random1Thru6();
//players() is a method that contains all the players in the team
//'place' refers to the players location in the array of players on the team
switch (randomInteger)
{
case 1: System.out.println(players(i).get(place) + " dances to celebrate his draft!");
break;
case 2: System.out.println(players(i).get(place) + " cartwheels to celebrate his draft!");
break;
case 3: System.out.println(players(i).get(place) + " throws a party to celebrate his draft!");
break;
case 4: System.out.println(players(i).get(place) + " sings to celebrate his draft!");
break;
case 5: System.out.println(players(i).get(place) + " gets root beer for everyone to celebrate his draft!");
break;
case 6: System.out.println(players(i).get(place) + " gets donuts to celebrate his draft!");
}
}
else if (place == 1) //Player is defensive
{
randomInteger = random1Thru6();
switch (randomInteger)
{
case 1: System.out.println(players(i).get(place) + " dances to celebrate his draft!");
break;
case 2: System.out.println(players(i).get(place) + " cartwheels to celebrate his draft!");
break;
case 3: System.out.println(players(i).get(place) + " throws a party to celebrate his draft!");
break;
case 4: System.out.println(players(i).get(place) + " sings to celebrate his draft!");
break;
case 5: System.out.println(players(i).get(place) + " gets root beer for everyone to celebrate his draft!");
break;
case 6: System.out.println(players(i).get(place) + " gets pizza to celebrate his draft!");
}
}
}
我应该对进攻和防守位置有一个不同的庆祝回应,如上面的代码所示。 这是引用的random1Thru6()方法:
public int random1Thru6() { //used to get a random number from 1 to 6
int randomInteger = (int)Math.random() * 10;
//this is supposed to call the method over and over again until it gives a number from 1-6 but it has been printing 7-9 as well
if (randomInteger > 6)
random1Thru6();
return randomInteger;
}
这是players()方法:
//holds and prints all the players
public ArrayList<String> players(int i) {
ArrayList<String> returnedList = new ArrayList<>();
// Christian McCaffrey is a Running Back, Corn Elder is a Corner Back for the Carolina Panthers
ArrayList<String> Players1 = new ArrayList<String>();
Players1.add("Christian McCaffrey");
Players1.add("Corn Elder");
//Jake Butt is a Tight End, Brendan Langley is a Corner Back for the Denver Broncos
ArrayList<String> Players2 = new ArrayList<String>();
Players2.add("Jake Butt");
Players2.add("Brendan Langley");
//Ryan Switzer is a Wide Receiver, Taco Charlton is a Defensive End for the Dallas Cowboys
ArrayList<String> Players3 = new ArrayList<String>();
Players3.add("Ryan Switzer");
Players3.add("Taco Charlton");
//Dalvin Cook is a Running Back, Ifeadi Odenigbo is a Defensive Line for the Minnesota Vikings
ArrayList<String> Players4 = new ArrayList<String>();
Players4.add("Dalvin Cook");
Players4.add("Ifeadi Odenigbo");
switch (i)
{
case 1: returnedList.addAll(Players1);
break;
case 2: returnedList.addAll(Players2);
break;
case 3: returnedList.addAll(Players3);
break;
case 4: returnedList.addAll(Players4);
break;
}
return returnedList;
}
以下是celeb()方法的调用方法:
for (int l = 0; l < players(i).size(); l++)
{
if (choosePlayer.equalsIgnoreCase(players(i).get(l)))
{
addPlayer(players(i).get(l));
celebrate(i, l);
enterRoster();
}
}
并且:
addPlayer(players(i).get(place));
celebrate(i, place);
enterRoster();
addPlayer(int i,int place)是一种为团队添加播放器的方法&#39; i&#39;在&#39; place&#39;的位置在团队的球员阵列中进入用户名单的ArrayList。
注意:我按照评论中的建议检查了random1Thru6()调用的号码,现在我明白为什么它不打印庆祝消息,因为我有(int)Math.random() * 10
它总是返回0所以我改成了:
double randomDouble = Math.random() * 10;
int randomInteger = (int)randomDouble;
现在它打印庆祝消息但random1Thru6()现在返回所有数字1-9,请解释我如何递归调用此方法本身,直到它返回数字1-6。
感谢delephin的评论!
注意:我已接受下面显示的delephin的答案,谢谢大家的帮助!
答案 0 :(得分:0)
添加到您的主要课程:
static Random r = new Random();
static {
r.setSeed(System.currentTimeMillis());
}
并将随机函数方法更改为以下内容:
public int random1Thru6() {
return r.nextInt(6) + 1;
}
通过快速测试,您之前的随机数发生器返回零。