Java 数组值的动态输入

Java 数组值的动态输入,java,arraylist,foreach,Java,Arraylist,Foreach,所以基本上我希望我的输出是这样的: 问题的数量取决于用户添加的问题数量,并存储在ArrayList中。这些选择也被动态输入并存储在一个数组中 问题是会发生这样的情况: 它在问题1中显示了我为问题2输入的选项,而不是显示它自己的选项集 这是开始游戏的代码: public static void startGame(){ int correctAnswers = 0; //used for later int count = 1, counter, System.out

所以基本上我希望我的输出是这样的:

问题的数量取决于用户添加的问题数量,并存储在
ArrayList
中。这些选择也被动态输入并存储在一个数组中

问题是会发生这样的情况:

它在问题1中显示了我为问题2输入的选项,而不是显示它自己的选项集

这是开始游戏的代码:

public static void startGame(){
    int correctAnswers = 0; //used for later
    int count = 1, counter, 
    System.out.println("===Have fun===");
    for(Question question: myQuestions){
        System.out.println("Question " +count);
        System.out.println(question.getQuestions());
        count++;
        counter=1;
        for(String i :question.getChoices()){
        System.out.printf("[%d]%s\n",counter,i);
        counter++;

        }
    }
}
这是添加问题的代码:

public static void addQuestion(){
    String[]choices = new String[3];
    String question = "";
    char yesorno;
    int answerIndex;

    System.out.println("==Add Question==");
    do{
    System.out.print("Enter question: ");
    try{
        question = console.readLine();
    }
    catch(IOException e){
        e.printStackTrace();
    }
    System.out.print("Enter choices <Fixed 3 choices> \n");
    for(int i=0; i<3; i++){
        System.out.printf("[%d] ",i);
        choices[i] = scan.next();

    }

    System.out.print("Enter the choice of the right answer [0,1 or 2}: ");
    answerIndex = scan.nextInt();
    Question quest = new Question(choices,question,answerIndex);
    myQuestions.add(quest);
    System.out.print("Do you want to add more questions?<Y/N> ");
    yesorno = Character.toLowerCase(scan.next(".").charAt(0));

    }while(yesorno == 'y');
    start();
}
publicstaticvoidaddquestion(){
字符串[]选项=新字符串[3];
字符串问题=”;
查尔·耶索诺;
国际应答指数;
System.out.println(“==添加问题==”);
做{
系统输出打印(“输入问题:”);
试一试{
question=console.readLine();
}
捕获(IOE异常){
e、 printStackTrace();
}
系统输出打印(“输入选项”\n);

对于(int i=0;i您正在更新每个问题对象中引用的choices值。在每个循环迭代结束时执行choices=new String[3],它将起作用。

解决它! 正如@Aitor所说,我正在更新addQuestion方法中do while循环的每次迭代中的String[]选项。我刚刚在do块的最后一部分添加了choices=new String[3],一切正常

这是我更新的addQuestion方法:

public static void addQuestion(){
    String[]choices = new String[3];
    String question = "";
    char yesorno;
    int answerIndex;

    System.out.println("==Add Question==");
    do{
    System.out.print("Enter question: ");
    try{
        question = console.readLine();
    }
    catch(IOException e){
        e.printStackTrace();
    }
    System.out.print("Enter choices <Fixed 3 choices> \n");
    for(int i=0; i<3; i++){
        System.out.printf("[%d] ",i);
        choices[i] = scan.next();   
    }

    System.out.print("Enter the choice of the right answer [0,1 or 2}: ");
    answerIndex = scan.nextInt();
    Question quest = new Question(choices,question,answerIndex);
    myQuestions.add(quest);
    System.out.print("Do you want to add more questions?<Y/N> ");
    yesorno = Character.toLowerCase(scan.next(".").charAt(0));
    choices = new String[3];//Solution. Thanks for this Aitor!
    }while(yesorno == 'y');
    start();
}
publicstaticvoidaddquestion(){
字符串[]选项=新字符串[3];
字符串问题=”;
查尔·耶索诺;
国际应答指数;
System.out.println(“==添加问题==”);
做{
系统输出打印(“输入问题:”);
试一试{
question=console.readLine();
}
捕获(IOE异常){
e、 printStackTrace();
}
系统输出打印(“输入选项”\n);

对于(int i=0;插入数据后,您是否查看了myQuestions变量?我没有发现代码中有任何错误。请按问题所在发布addQuestion方法there@k4yaman通过实验,我通过一种方法来观察,第二个问题的选择也记录在第一个问题上!看起来我的addQuestion记录最后输入的选项,并将它们分配给所有问题。:vTanks!刚刚搞定!
public static void addQuestion(){
    String[]choices = new String[3];
    String question = "";
    char yesorno;
    int answerIndex;

    System.out.println("==Add Question==");
    do{
    System.out.print("Enter question: ");
    try{
        question = console.readLine();
    }
    catch(IOException e){
        e.printStackTrace();
    }
    System.out.print("Enter choices <Fixed 3 choices> \n");
    for(int i=0; i<3; i++){
        System.out.printf("[%d] ",i);
        choices[i] = scan.next();   
    }

    System.out.print("Enter the choice of the right answer [0,1 or 2}: ");
    answerIndex = scan.nextInt();
    Question quest = new Question(choices,question,answerIndex);
    myQuestions.add(quest);
    System.out.print("Do you want to add more questions?<Y/N> ");
    yesorno = Character.toLowerCase(scan.next(".").charAt(0));
    choices = new String[3];//Solution. Thanks for this Aitor!
    }while(yesorno == 'y');
    start();
}