3
    private String[][] questions = 
    {
        {"Sky's color is black", "false"},
        {"The earth is round.", "true"}, 
        {"Google is written in PHP", "false"},
        {"This program is written in JAVA", "true"}, 
        {"Daniel is white", "true"}
    };

    public void selectRandomQuestion() {

    }

我希望系统从数组中随机选择一个问题,选择后,我想打印它+让系统记住这个问题已经打印了,这样系统就不会再选择那个问题了。

例子:

我收到了“Google 是用 PHP 编写的”的问题,回答了它,现在我无法再收到这个问题了。在我用完问题后,系统会转动boolean game to false,因此游戏将结束。

我怎样才能做到这一点?

4

6 回答 6

3

到目前为止,实现这一点的最简单方法(但不是我推荐的)是使用 an Arraylistof Strings,然后删除使用过的问题

public class Test { 
    public static void main(String[] args) { 

        Random rnd=new Random();
        ArrayList<String> questions = new ArrayList<String>();
        ArrayList<Boolean> answers = new ArrayList<Boolean>();

        questions.add("Question 1");
        answers.add(true);

        questions.add("Question 2");
        answers.add(false);

        while (array.isEmpty()==false){
            int index=rnd.nextInt(questions.size());

            String question=questions.get(index);
            boolean answer=answers.get(index);

            questions.remove(index);
            answer.remove(index);

            //do whatever with the question
        }


    }

}


面向对象的替代方案
然而,一个更好的面向对象的方法是创建一个对象来将问题和答案放在一起

public class QAndA {
    public final String question; 
    public final boolean answer;

    public QAndA(String question, boolean answer) {
        this.question = question;
        this.answer = answer;
    }


}

然后将这些物体放在一个Arraylist

public class Test { 
    public static void main(String[] args) { 

        Random rnd=new Random();

        ArrayList<QAndA> array = new ArrayList<QAndA>();

        array.add(new QAndA("Question 1",true));
        array.add(new QAndA("Question 2",true));


        while (array.isEmpty()==false){
            int index=rnd.nextInt(array.size());

            QAndA question=array.get(index);
            array.remove(index);

            //do whatever with the question
        }


    }

}

从 an 中删除对象Arraylist不是一个非常快速的选择,但鉴于Arraylist可能很短,这不太可能是一个重要因素。如果是考虑其他一些集合。中的字段QAndA被声明publicQAndA类是一个美化的结构,再次考虑这是否合适取决于您的用法。

于 2013-07-03T19:13:51.380 回答
1

正如其他人所建议的那样,从 ArrayLists 中删除以前选择的项目是可行的。另一种解决方案是打乱你的数组,然后遍历它。一旦你到最后,你就会以随机顺序触及每个问题一次。

于 2013-07-03T21:41:43.543 回答
0

请参阅Random类 javadoc 和结帐 Set<Integer>

于 2013-07-03T19:02:11.237 回答
0

您可以创建一个 arrayList 索引问题并具有布尔值,该值指示是否已被询问。要查看游戏何时结束,只需创建一个等于 questions.length 的变量并迭代多次。

如果您反对使用 arrayList,您可以在数组中添加第三行:

private String[][] questions = 
{
    {"Sky's color is black", "false", "false"},
    {"The earth is round.", "true", "false"}, 
    {"Google is written in PHP", "false", "false"},
    {"This program is written in JAVA", "true", "false"}, 
    {"Daniel is white", "true", "false"}
};

最后一行可以指出问题是否被访问。只是为了澄清第一个问题“天空的颜色是黑色”:

questionAsked = questions[0][0]
answer = questions[0][1]
ifAsked = questions[0][2]
于 2013-07-03T19:08:07.417 回答
0

您可以将 Objects 插入 Set,它可以是HashSet。一个 Set 将维护唯一项目的列表。HashSet 将维护一组唯一的问题对象。我还覆盖了 equals 方法,因为它是由 HashSet 调用的唯一性。

      public class Test { 
        public static void main(String[] args)
      { 

     HashSet<Question> setOfItems = new HashSet<String>();
        setOfItems.add(new Question("Sky's color is black",true));
        setOfItems.add(new Question("The earth is round.",false));
        setOfItems.add(new Question("Google is written in PHP",false));
        setOfItems.add(new Question("This program is written in JAVA",true));
        setOfItems.add(new Question("Daniel is white",false));
        
       while(!setOfItems.isEmpty())
       {
            Question selectedQuestion = selectRandomQuestion(setOfItems);
            //DO whatever needed.
       }
     public Question selectRandomQuestion(HashSet<Question> setOfItems) {
        String result = null;
        if(setOfItems.size() >0)
        {
            Random generator = new Random( );
            int index = generator.nextInt( setOfItems.size() );
            Iterator<Question> i = setOfItems.iterator(); 
            
            while(index<0)
            {
                 result = i.next();
                 index--;
            }
            setOfItems.remove(result); //Remove from Set
            
         }
         return current;
     }
   }
}


public class Question
        {
              private String question;
              private boolean answer;
              public Question(String question, boolean answer)
              {
                   this.question = question;
                   this.answer = answer;
              }
              @Override
              public boolean equals(Question q)
              {
                   return this.question.equals(q.getQuestion());
              }
              //Getter Setters
        }
于 2013-07-03T19:23:44.010 回答
0

您可以通过创建一个代表每个问题的类和一个用于绘制随机问题的类来使其有点面向对象。

代表问题的类 public class Question{

private boolean notShown = true;
private String question;

public Question(String s){

    this.question = s;
}
public boolean isNotShown(){

    return notShown;
}
public String GetQuestion(){


    notShown = false;
    return question;
}

public void setQuestionNotSeen(){

    notShown = true;
}
}

代表抽奖的类

public class QuestionLister {


private ArrayList<Question> listOfQuestions;

public QuestionLister() {

    listOfQuestions = new ArrayList<>(0);

}
public QuestionLister(String s) {

    listOfQuestions = new ArrayList<>(0);
    listOfQuestions.add(new Question(s));

}


public void addQuestion(String s){

    listOfQuestions.add(new Question(s));
}

public String getRandomQuestion(){
    int r;
    boolean allSeen = true;

    for (Question question : listOfQuestions) {

        if(question.isNotShown()){

            allSeen = false;
        }
    }

    if(!allSeen){
        do{
            r = (int) Math.random() * listOfQuestions.size();

        } while(!listOfQuestions.get(r).isNotShown());

        return(listOfQuestions.get(r).GetQuestion());
    }

    return "All questions shown";
}

public void setAllQuestionsNotSeen(){

    for (Question question : listOfQuestions) {
        question.setQuestionNotSeen();
    }
}
}
于 2013-07-03T19:34:32.173 回答