Results 1 to 3 of 3
  1. #1
    hezfast2 is offline Member
    Join Date
    May 2008
    Posts
    4
    Rep Power
    0

    Default vector list search

    Hello, I've written a multiple choice question version of a Hangman game. Everything works 'except' I don't want questions asked more than once. I've come up with a search algorithm, but I obviously don't have it correct, or I'm going wrong somewhere, It compiles but the questions still repeat. here's what I have so far: Any help or advice would be greatly appreciated.
    Java Code:
    		questionNum = (int)(Math.random() * 10);  
    		while (isUsed)
    		{
    		searchResult = seqSearch(usedList, questionNum);
    		if (searchResult == -1)
    		{
    		    isUsed = false;
          }//end if
    		else
    		    questionNum = (int)(Math.random() * 10);
    		usedList.addElement(questionNum);
    		}//end while
    		QuestionInfo curQues = new QuestionInfo(questionObj[questionNum]);
    		
    		displayQuestion(questionObj, curQues, questionNum);
    Java Code:
    	public int seqSearch(Vector<Integer> usedList, int questionNum)
    	{
    		 boolean found = false;
    		 
    		 for (Integer num : usedList)
    		     if (num == questionNum)
    			  {
    			      found = true;
    					break;
    			  }
    		 if (found)
    		     return 1;
    			  else
    			     return -1;
    	}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Personally, I'd not use a Vector, but instead would use a ArrayList of Question, and then call Collections.shuffle(...) on my ArrayList. Then with a shuffled list simply loop through the list asking questions.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Cross-posted in the Sun Java forums. Please don't cross-post unless you are planning to upset folks in the various fora.

Similar Threads

  1. Search a object in a vector
    By TalhaS in forum New To Java
    Replies: 2
    Last Post: 04-30-2008, 03:05 PM
  2. How to Search a List in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:38 PM
  3. Vector help
    By king_arthur in forum New To Java
    Replies: 3
    Last Post: 01-22-2008, 08:33 PM
  4. Vector capacity
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-04-2007, 06:58 PM
  5. array vs Vector
    By paty in forum New To Java
    Replies: 1
    Last Post: 08-02-2007, 08:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •