2016-12-27 69 views
1

我在嘗試將構造函數方法中指定的StringString[]保存到數組中然後將其打印出來時遇到問題。 該計劃背後的想法是添加一個人的塗鴉程序與他們的名字和他們的答案(在voegDeelnameToe),然後打印出所有信息槽geefPollInfo()Java:從數組中的方法保存指定的字符串

我試圖加6人線槽voegDeelnameToe()方法,但我不能保存
String deelnemer(=字符串 '參與者的名字')

String[] aanwezigheden(=的String []「答案')

在另一個對象中,然後將其打印出來。

doodle.voegDeelnameToe("Bram",new String[]{"ja", "ja", "nee", "ja", "ja", "nee", "ja", "ja", "ja", "nee", "nee", "nee", "ja", "ja", "ja"}); 
doodle.voegDeelnameToe("Katja", new String[]{"nee", "nee", "ja", "nee", "nee", "nee", "ja", "nee", "ja", "ja", "nee", "ja", "ja", "nee", "ja"}); 
doodle.voegDeelnameToe("Kristien", new String[]{"ja", "ja", "ja", "ja", "nee", "nee", "nee", "nee", "nee", "ja", "nee", "ja", "ja", "nee", "ja"}); 
doodle.voegDeelnameToe("Peter", new String[]{"ja", "nee", "ja", "ja", "nee", "nee", "nee", "nee", "nee", "ja", "nee", "ja", "ja", "nee", "ja"}); 
doodle.voegDeelnameToe("Sven", new String[]{"nee", "nee", "ja", "ja", "nee", "nee", "nee", "nee", "nee", "ja", "nee", "ja", "ja", "ja", "ja"}); 
doodle.voegDeelnameToe("Matthias", new String[]{"ja", "ja", "ja", "ja", "nee", "ja", "nee", "nee", "nee", "ja", "nee", "ja", "ja", "nee", "ja"}); 
System.out.println(doodle.geefPollInfo());` 

類:

public class Poll { 

    boolean type; 
    String titel; 
    String omschrijving; 
    String locatie; 
    String initiator; 
    final int MAX_DEELNEMERS = 10; 
    LocalDate[] data; 
    LocalTime[] tijdstippen; 
    String[] deelnemers = new String[] {"","","","","","",}; 
    String[] antwoorden = new String[15]; 
    String[][] poll = new String[6][15]; 
    String einde = " "; 
    boolean afgesloten; 
    int teller = 1; 

    public Poll(String testBasispoll_1, String dit_is_de_eerste_basispoll, String location, String name, LocalDate[] data, LocalTime[] tijdstippen, boolean b) { 
     titel = testBasispoll_1; 
     omschrijving = dit_is_de_eerste_basispoll; 
     locatie = location; 
     initiator = name; 
     this.data = data; 
     this.tijdstippen = tijdstippen; 
     type = b; 


    } 


    public String geefPollInfo() { 

     System.out.println("Titel : " + titel); 
     System.out.println("Omschrijving : " + omschrijving); 
     System.out.println("Locatie : " + locatie); 
     System.out.println("Initiator : " + initiator); 
     System.out.println("Max aantal deelnemers : " + MAX_DEELNEMERS); 
     System.out.println("--------------------------------------------------------"); 
     System.out.println("Dag \t \t" + data[0] + "\t \t" + data[1] + "\t \t" + data[2] + "\t \t" + data[3] + "\t \t" + data[4]); 
     System.out.println("Uur \t \t" + tijdstippen[0] + "\t" + tijdstippen[1] + "\t" + tijdstippen[2] + "\t" + tijdstippen[0] + "\t" + tijdstippen[1] + "\t" + tijdstippen[2] + "\t" + tijdstippen[0] + "\t" + tijdstippen[1] + "\t" + tijdstippen[2] + "\t" + tijdstippen[0] + "\t" + tijdstippen[1] + "\t" + tijdstippen[2] + "\t" + tijdstippen[0] + "\t" + tijdstippen[1] + "\t" + tijdstippen[2]); 


     for (int i = 0; i < 6; i++) { 
      System.out.println(deelnemers[i] + "\t"); 
     } 

      for (int j = 0; j < 15; j++) { 
       System.out.print(antwoorden[j] + "\t"); 

      } 




     return einde; 
    } 

    public boolean voegDeelnameToe(String deelnemer, String[] aanwezigheden) { 

     //for (int i = 0; i < 6; i++) { 
      for (int j = 0; j < 15; j++) { 
       antwoorden[j] = aanwezigheden[j]; 
      }    
     // } 
     for (int i = 0; i < 6; i++) { 
      deelnemers[i] = deelnemer; 

     } 


     return type; 
    } 
+0

您正在從geefPollInfo()打印返回的字符串,它是變量einde。 einde只是一個「角色」,我不認爲它在任何地方都會重新初始化。我不確定你在這裏做什麼。 –

回答

0

方法voegDeelnameToe將(以上)寫在每次調用時antwoorden的antwoorden所有元素。因此只有最後一個人Matthias將保留在民意調查。建議:

  1. 修改voegDeelnameToe簽名類似boolean voegDeelnameToe(int personIndex, String deelnemer, String[] aanwezigheden)。然後跳過第二個循環,只做deelnemers[index] = deelnemer;。我不確定你在這種情況下應該怎麼處理aanwezigheden

  2. Poll創建一個域int index = 0;每次voegDeelnameToe增加它,只要它是低於6例如被稱爲:

    deelnemers[index] = deelnemer; 
    ++index;` 
    

我要指出的是,有一個場String[][] poll = new String[6][15];Poll類,似乎它應該保持投票結果。你可能應該在那裏存儲aanwezigheden爲每個參與者?否則每次都會覆蓋antwoorden。例如,如果你在int index = 0;Poll添加一個字段,方法voegDeelnameToe會是這個樣子:

public boolean voegDeelnameToe(String deelnemer, String[] aanwezigheden) { 
    if (index < poll.length && index < deelnemers.length) { 
     deelnemers[index] = deelnemer; 
     System.arraycopy(aanwezigheden, 0, poll[index], 0, aanwezigheden.length); 
     ++index; 
    } 
    return type; 
} 

請注意,您應該檢查一下指數是在這種情況下,數組邊界之內,並且System.arraycopy是更有效和可以說比for循環複製數組簡單得多。

+0

是的,最初的目的是在String [] [] poll = new String [6] [15]中存儲aanwezigheden和antwoorden;因爲我無法弄清楚,我嘗試了不同的方式。如果你有任何建議找到一個解決方案的String [] [] poll = new String [6] [15];這將非常感激。 – FlexTec

相關問題