2012-03-16 9 views
-2

ランダムな質問を生成するこのメソッドを使用しているので、各質問を1回だけ生成することができます。 どうすればいいですか?文字列が一度だけ生成されることを確認する方法

これは、これまでのコードです:

package boss; 
import java.util.Random; 
import javax.swing.JFrame; 


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

     LoginWindow window = new LoginWindow(); 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     window.setVisible(true); 
    } 

    public String getQuestions() { 
     String [] question = new String[30]; 
     question[0] = "hello"; 
     question[1] ="yo"; 
     question[2] ="b"; 
     question[3] ="ha"; 

     //Generating random questions 
     Random r = new Random(); 
     int i=r.nextInt(4); 
     String quest=question[i]; 
     return quest; 
    } 
} 
+3

あのを、あなたはすでに選んだものを追跡します。 –

+0

ArrayListを作成し、リストにない場合は追加します。それ以外の場合は再ループします。 –

答えて

1

あなたはすでにきたもののリストを維持する必要があります使用し、それに対してチェックする。

boolean used[] = new boolean[30]; 
int i; 

do { 
    Random r = new Random(); 
    i=r.nextInt(4); 

} while(used[i] == true); 

String quest=question[i]; 
used[i] = true; 
4

ではなく、テーブルのArrayListのを使用してください。表示された質問をArrayListから削除します。

+0

質問数が少なくなったため、生成される乱数の範囲を変更してください。 –

1

かなり単純な解決策は、あなたが求めているすべての質問を記録しておくだけで、あなたが持っていないものを生成するために、次のようになります。

private ArrayList<Integer> questionsAsked = new ArrayList<>(); 

public String getQuestions() 
{ 
    String [] question = new String[30]; 
    question[0] = "hello"; 
    question[1] ="yo"; 
    question[2] ="b"; 
    question[3] ="ha"; 

    //Generating random questions 
    Random r = new Random(); 
    int i = r.nextInt(question.length); 

    //keep looping until you find a question you have not asked  
    while(questionsAsked.contains(i)) 
    { 
     i = r.nextInt(question.length); 
    } 

    //add that question to the list of questions already asked 
    questionsAsked.add(i); 

    //ask the question 
    return question[i]; 
} 
0

あなたが既に選んだものを記録してください。

String [] question = new String[30]; 
boolean[] picked = new boolean[30]; 
... 
if (!picked[i]) 
{ 
    String quest=question[i]; 
    picked[i] = true; 
} 
else 
    // choose another 

5

あなたはあなたの例では質問が発生していない(もちろん、あなたのコードを再構築し、また、あなたの質問の供給をexhausedきたし、すべてが選ばれたときに知って対処する必要があります) - 配列に格納された固定セットからそれらを選択します。必要な数の質問が表示されるまで、配列をシャッフルして、その一部を繰り返し処理しているように思えます。したがって、質問をシャッフルしてから、シャッフルされた配列を繰り返したり、インデックス0..nの配列をシャッフルして、元の質問リストにあるものを繰り返します。

シャッフルにはさまざまなアプローチがありますが、入力データを1回パスして、各要素を他のランダムに選択した要素と入れ替えるのが最も簡単な方法でしょう。

+3

shuffle()の+1、ここに示す(http://stackoverflow.com/a/2524394/230513)。 – trashgod

1

あなたはキューの削除と怠惰な質問生成、実施例と一緒にCollections.shuffleを使用することができます:あなたは「シャッフル」アルゴリズムでこれを解決することができ

$ javac Mkt.java && java Mkt 
ha 
yo 
hello 
b 
b 
ha 
hello 
yo 
hello 
ha 
1

import java.util.*; 

public class Mkt { 
    private Queue<String> questions = null; 

    public Mkt() { 
    for(int i = 0; i < 10; i++) { 
     System.out.println(getQuestion()); 
    } 
    } 

    public String getQuestion() { 
    if(questions == null || questions.size() == 0) { 
     questions = generateQuestions(); 
    } 
    return questions.remove(); 
    } 

    private Queue<String> generateQuestions() { 
    List<String> list = Arrays.asList("hello", "yo", "b", "ha"); 
    Collections.shuffle(list); 
    return new LinkedList<String>(list); 
    } 

    public static void main(String[] args) { 
    new Mkt(); 
    } 
} 

サンプル実行。基本的にあなたの配列をランダム化(シャッフル)し、リストから次の項目を選択するだけです。あなたの配列シャッフルするhttp://en.wikipedia.org/wiki/Knuth_shuffle

擬似コード:最も簡単なシャッフルアルゴリズムの

一つはKnuthのある

Random rand = new Random(); 
    for (int i=questions.Length-1; i>=0; --i) 
    { 
     int nextRand = rand.Next(i); 

     // Switch the randomly selected 'next' to the current pointer in the array 
     string temp = questions[nextRand]; 
     questions[nextRand] = i; 
     questions[i] = temp; 
    } 
関連する問題