Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 问答题重复问题_Java_Android_For Loop_While Loop_Repeat - Fatal编程技术网

Java 问答题重复问题

Java 问答题重复问题,java,android,for-loop,while-loop,repeat,Java,Android,For Loop,While Loop,Repeat,我是Android Java编程新手,我的问答题(总共50个问题)经常重复出现问题。我的问题和答案将添加到地图中。然后我调用我的方法setQuestion()来显示我的问题和答案,它们都工作得很好 Map<String, String> userQuestions = new HashMap<String, String>(); int questionNumber = 1; //all my questions go here setQuestion(); }

我是Android Java编程新手,我的问答题(总共50个问题)经常重复出现问题。我的问题和答案将添加到地图中。然后我调用我的方法setQuestion()来显示我的问题和答案,它们都工作得很好

Map<String, String> userQuestions = new HashMap<String, String>();

int questionNumber = 1;

//all my questions go here

setQuestion();
}

public void setQuestion() 
{

Random randomQuestions = new Random();             
    List questions = new ArrayList<>();                          
    List repeatedQuestions = new ArrayList<>();         
    for (int i = 0; i <questionNumber; i++) 
       {
        while (true) 
          {
            questionNumber = randomQuestions.nextInt(50) +1;
            if (!questions.contains(randomQuestions)) 
              {                                                                                        
                repeatedQuestions.add(repeatedQuestions);
                break;
              }
           }
        }    
} 
Map userQuestions=newhashmap();
整数=1;
//我所有的问题都在这里
设置问题();
}
公共问题
{
随机问题=新随机();
列出问题=新建ArrayList();
List repeatedQuestions=new ArrayList();

对于(inti=0;i在Hashmap中存储问题不是一个好主意,您可以尝试使用raraylistofcastle(Structure)存储小数据,使用数据库存储大数据

现在要生成随机问题,每次都可以使用洗牌方法

示例

 public void shuffle_ques() {

     // arrQuestions to store to all Questions and Answers
     // arrQuestions.size() gives total number of questions in arrQuestions

    for (int i = 0; i < arrQuestions.size(); i++) 
    { 
        this.arrshuffle.add(i); // arrshuffle to store all the values of i
    }

    shuffle(arrshuffle); // shuffling arrshuffle so we get rearranged values of i in array

}

此代码将在应用程序中以及重新启动应用程序时加载随机问题。

可能您的while语句中存在问题?如果问题是重复的,则大多数原因是while语句。当您进行提问时。包含(随机问题)这并不是你想的那样。你真的应该使用嵌套for循环。但我也很难理解你的代码,因为它的语法不正确。如果你有一个问题列表,比如说1000个问题,然后你想随机选择50个。你可以做一些类似于基于50的for循环的事情。使用random()获取介于1-1000之间的随机数。一旦获得,请列出。获取(随机数)回答这个问题。这是最简单的方法,如果你需要看代码,请告诉我。我为我的语法道歉,如果你能给我看代码,我将不胜感激。谢谢。我有50个问题想看,然后让测验结束。请帮帮忙,我尝试了许多不同的方法,我无法防止重复是的。散列映射是否无法防止重复条目?。非常感谢您编写代码并对其进行解释。我确实同意您的方法是更有效的方法。我只是想知道,当我的测验快结束时,为什么我的for循环无法工作?我不明白为什么它没有将以前提出的问题添加到我的for数组中r正在检查,再次感谢。
Random randomQuestions = new Random();
List<Integer> questions = new ArrayList<Integer>();

Integer questionNumber = randomQuestions.nextInt(50) + 1;
    if (!questions.contains(questionNumber))
    {
       questions.add(questionNumber);
    }                                                                                   
 public void shuffle_ques() {

     // arrQuestions to store to all Questions and Answers
     // arrQuestions.size() gives total number of questions in arrQuestions

    for (int i = 0; i < arrQuestions.size(); i++) 
    { 
        this.arrshuffle.add(i); // arrshuffle to store all the values of i
    }

    shuffle(arrshuffle); // shuffling arrshuffle so we get rearranged values of i in array

}
 public void LoadQues() {

    // int count = 0; (Global)
    // when calling LoadQues() method set count++ for next random ques.
    /* setting ques on textview by getting ques from arrQuestions
       and getting any random index number for question from arrshuffle.*/

    txtques.setText(arrQuestions.get(arrshuf.get(count)).Ques);
    btn1.setText(arrQuestions.get(arrshuf.get(count)).Opt1);
    btn2.setText(arrQuestions.get(arrshuf.get(count)).Opt2);
    btn3.setText(arrQuestions.get(arrshuf.get(count)).Opt3);
    btn4.setText(arrQuestions.get(arrshuf.get(count)).Opt4);
    ans = arrQuestions.get(arrshuf.get(count)).Ans;


}