2017-06-15 131 views
-2

我需要實現一個接口作爲我的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; 
    } 

這裏是玩家()方法:

//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; 

    } 

這裏是如何慶祝()方法被調用:

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)是一種方法,將球隊'我'的球員加入球隊球員陣列中'地點'的位置,加入到用戶名單的陣列列表中。

注意:我查看了random1Thru6()中正在調用的註釋中的數字,現在我明白了它爲什麼不打印慶祝消息,因爲我有(int)Math.random() * 10它總是返回0,所以我將其更改爲:

double randomDouble = Math.random() * 10; 
int randomInteger = (int)randomDouble; 

現在它打印慶祝的消息,但random1Thru6()現在返回所有數字1-9,請解釋我怎麼能做出這種方法調用自身遞歸,直到它會返回一個數字1-6。

謝謝delephin您的評論!

注:我已經接受delephin的答案如下所示,謝謝大家的幫助!

+1

除了默認方法的情況下,接口不做任何事情。這聽起來像你正在編寫一個可能實現接口的類。你能澄清嗎? – CollinD

+0

@CollinD謝謝你,我編輯了我的問題。我希望它能簡化問題。 –

+6

代碼不足以提供幫助。可能的原因是什麼都沒有印出:1)你不叫這個方法。 --- 2)'place'的值不是0或1. --- 3)'random1Thru6()'返回範圍1-6以外的值。 ---我們可以給出的最佳幫助是:[什麼是調試器,它如何幫助我診斷問題?](https://stackoverflow.com/q/25385173/5221149) – Andreas

回答

0

添加到您的主類:

static Random r = new Random(); 

static { 
    r.setSeed(System.currentTimeMillis()); 
} 

,改變你的隨機方法是這樣的:

public int random1Thru6() { 
    return r.nextInt(6) + 1; 
} 

從快速測試,你以前的隨機返航零。

+0

謝謝你的回答!該方法正在工作,正在生成正確的消息。 –

+1

爲什麼要設置種子?真的需要嗎? –

+0

確實沒有。 Javadoc說:「這個構造函數將隨機數生成器的種子設置爲一個非常有可能不同於此構造函數的任何其他調用的值。」所以不需要那個靜態塊。 –