数组动态输入值

时间:2015-11-15 01:05:38

标签: java arraylist foreach

所以基本上我想让我的输出像这样:

Desired output

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

问题是这是发生了什么:

Current Output

它在问题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();
}

考虑这些声明(仅供参考):

//declared in the main class.
static ArrayList<Question> myQuestions = new ArrayList<Question>();
static BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
static Scanner scan = new Scanner(System.in);

//Question class
public class Question {
private String[]choices = new String[3];
private String questions;
private int answerIndex;

public Question(String[] choices, String questions, int answerIndex){
    this.choices = choices;
    this.questions = questions;
    this.answerIndex = answerIndex;
}
public String[] getChoices(){
    return choices;
}
public String getQuestions(){
    return questions;
}
public int getAnswerIndex(){
    return answerIndex;
}

}

提前谢谢你们! :d

2 个答案:

答案 0 :(得分:0)

您正在更新每个问题对象中引用的选项值。在每次循环迭代结束时做一个选择= new String [3]并且它将起作用

答案 1 :(得分:0)

想出来! 正如@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();
}

非常感谢帮助。 Stack Overflow(通过我们的程序员)永远不会帮助我们所有人。非常尊重! :)直到下一次