Java:从数组中的方法保存分配的字符串

时间:2016-12-27 14:50:34

标签: java arrays string methods constructor

我在尝试将指定的StringString[]从构造函数方法保存到数组中然后将其打印出来时遇到了问题。 该计划背后的想法是在涂鸦程序中添加一个人的姓名和答案(在voegDeelnameToe中),然后通过geefPollInfo()打印出所有信息。

我试图通过voegDeelnameToe()方法添加6个人,但我无法保存 String deelnemer(=字符串'参与者姓名')

String[] aanwezigheden(=字符串[]'答案')

在另一个对象中,然后打印出来。

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

1 个答案:

答案 0 :(得分:0)

方法voegDeelnameToe每次调用时都会(过)写入 antwoorden antwoorden 的所有元素。因此,只有最后一个人 Matthias 将保留在民意调查中。意见建议:

  1. voegDeelnameToe签名修改为boolean voegDeelnameToe(int personIndex, String deelnemer, String[] aanwezigheden)之类的内容。然后跳过第二个for循环,然后执行deelnemers[index] = deelnemer;。在这种情况下,我不确定你应该对aanwezigheden做什么。

  2. int index = 0;中创建一个字段Poll,并在每次调用voegDeelnameToe时将其递增,只要它低于6. E.g:

    deelnemers[index] = deelnemer;
    ++index;`
    
  3. 我必须指出String[][] poll = new String[6][15];类中有一个字段Poll,它似乎应该保存投票结果。您可能应该为6位参与者中的每一位存储aanwezigheden?否则antwoorden每次都会被覆盖。例如,如果您在int index = 0;的{​​{1}}中添加字段,则方法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; } 比复制数组的for循环更有效且更简单。