2013-02-23 84 views
3

我想顯示四個數字選項,就像測驗選項一樣,四個三個中應該是錯誤的,一個應該是正確的答案。所有選項都是數字編號,每次頁面打開時我都想更改正確答案的位置。請幫我我想顯示四個隨機數,最小的一個最大值爲十,但包含一個數字修正

,我的結果是一個字符串結果,在這裏我將添加結果.....

我想這一點,但沒有得到解決

LinearLayout rowoptions = (LinearLayout) findViewById(R.id.linearlayout); 
ArrayList<Integer> numbers = new ArrayList<Integer>(); 
String[] s = new String[4]; 
while (numbers.size() < 4) { 
    int random = r.nextInt(10)+1; 
    int chk = r.nextInt(4)+1; 
    if (!numbers.contains(random)) { 
     numbers.add(random); 
     s[chk] =String.valueOf(random); 
    } 
} 

for (int i = 0; i < 4; i++) { 
    Button optionbutton = new Button(this); 
    optionbutton.setText(s[i]); 
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(70, 70); 
    layoutParams.setMargins(5, 5, 0, 0); // left, top, right, bottom 
    optionbutton.setLayoutParams(layoutParams); 
    //ivBowl.setBackgroundDrawable(null); 

    rowoptions.addView(optionbutton); 
} 
+0

這是你的要求,你想顯示四個隨機數字按鈕,其中一個是答案,但我想知道你的答案在哪裏..? – Pragnani 2013-02-23 07:52:07

+0

@Pragnani你是對的,我想添加我的答案,但我不明白我會添加我的答案,我的答案是字符串結果 – Rohit 2013-02-23 07:54:04

+0

儘管你有示例代碼(太棒了!),但你沒有明確表示你的投入和預期的產出。具有更詳細的示例對了解您的挑戰是非常有幫助的。例如,我將輸入的答案編碼爲1,2,3,4(其中1總是正確的答案),然後在輸出上隨機化這個序列,得到類似3,4,1,2的東西。 – mvp 2013-02-23 08:08:11

回答

1

喜做你需要這樣的東西

LinearLayout layout=(LinearLayout)findViewById(R.id.mainlayout); 
ArrayList list=new ArrayList(); 
    list.add(answer); 
    Random r=new Random(); 
    for(int i=0;i<3;i++) 
    { 
     while(true) 
     { 
      int next=r.nextInt(10)+1; 
      if(!list.contains(next)) 
      { 
      list.add(next); 
      break; 
      } 
     } 
    } 
    Collections.shuffle(list); 
    for(int i=0;i<list.size();i++) 
    { 
     final Button b=new Button(this); 
     b.setText(list.get(i).toString()); 

     b.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       int selectedAnser=Integer.parseInt(b.getText().toString()); 
       if(!(selectedAnser==answer)) 
       { 
        Toast.makeText(context, "Wrong Answer", Toast.LENGTH_SHORT).show(); 
       } 

      } 
     }); 
     layout.addView(b); 
    } 
+0

感謝兄弟,多麼真棒的答案......非常感謝 – Rohit 2013-02-23 09:04:27

相關問題