2014-05-30 25 views
-1

我想知道我可以如何匹配我的兩個數組,其猜測字母,它的一個小寫字母和4大寫字母的遊戲,我希望大寫字母之一應該是相同的小寫,所以我可以點擊匹配的信件! thnks的答案!一個字母猜謎遊戲的孩子android

package com.example.alfabetet; 
import java.util.*; 

import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.widget.TextView; 

public class AlfaSpelet extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.alfaspelet); 

     String gemen[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "å", "ä", "ö" }; 

     String alfa[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "Å", "Ä", "Ö" }; 



     TextView txt1 = (TextView) findViewById(R.id.textView2); 
     TextView txt2 = (TextView) findViewById(R.id.textView3); 
     TextView txt3 = (TextView) findViewById(R.id.textView4); 
     TextView txt4 = (TextView) findViewById(R.id.textView5); 

     TextView txt5 = (TextView) findViewById(R.id.textView6);   

     Random random = new Random(); 

      int rnd1 = random.nextInt(alfa.length); 
      int rnd2 = random.nextInt(alfa.length); 
      int rnd3 = random.nextInt(alfa.length); 
      int rnd4 = random.nextInt(alfa.length); 

      int rnd5 = random.nextInt(gemen.length); 

      txt1.setText(alfa[rnd1]); 
      txt2.setText(alfa[rnd2]); 
      txt3.setText(alfa[rnd3]); 
      txt4.setText(alfa[rnd4]); 
      txt5.setText(gemen[rnd5]);`enter code here` 
+1

rnd5應該是一個從RND1到rnd4 ......使[] RND = INT新[4]提出的隨機數有則INT選擇INT =中隨機選擇0〜 3;然後選擇gemen [rnd [selected]] ...記住當你調用random.nextInt(alfa.length)時,你可以多次獲得相同的數字...所以也許更好的是它將洗牌int從0到alfa.length並取前4個元素 – Selvin

回答

0

試試這個:

int rnd1 = random.nextInt(alfa.length); 
int rnd2 = random.nextInt(alfa.length); 
int rnd3 = random.nextInt(alfa.length); 
int rnd4 = random.nextInt(alfa.length); 

int rnd[] = {rnd1, rnd2, rnd3, rnd4}; 

int rnd5 = random.nextInt(rnd.length); 

txt1.setText(alfa[rnd1]); 
txt2.setText(alfa[rnd2]); 
txt3.setText(alfa[rnd3]); 
txt4.setText(alfa[rnd4]); 
txt5.setText(gemen[rnd[rnd5]]); 
+0

否....不是'txt5.setText(gemen [rnd5]);'...它應該是'txt5.setText(gemen [rnd [rnd5]]);' – Selvin

+0

道歉...我已經編輯它來反映你的更正。 –

+0

它應該是整數列當然:) – Selvin