2015-06-26 26 views
0

所以我試圖做到這一點:當你點擊圖像時,中心的文字變成了數組中的一個隨機元素,但我想每一個出現一次的元素都不再出現...我嘗試這樣做,但它不起作用,請幫助我在這裏!試圖讓數組元素只出現一次

package com.example.bibiwars.skills; 


import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 


import java.util.Random; 


public class MainActivity extends ActionBarActivity { 

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


      ImageView img = (ImageView) findViewById(R.id.logo); 
      img.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      //ToastMessage to test the click :::::: 
      Toast.makeText(MainActivity.this, "Clicked!!",Toast.LENGTH_SHORT).show(); 


      TextView text = (TextView) findViewById(R.id.text); 
      String[] Array = {"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"}; 
      int rnd = new Random().nextInt(Array.length); 
      while (Array[rnd].equals("0")) 
       {rnd = new Random().nextInt(Array.length);} 
      text.setText(Array[rnd]); 
      Array[rnd] = "0"; 
     } 

    }); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 



} 
+0

將右括號從'rnd = ...;'行後移到'Array [rnd] =「0」;'之後。由於印刷錯誤而投票結束,作爲題外話題。順便說一句,簡單的調試會告訴你這個問題。嘗試學習如何調試。 – Jashaszun

+0

我甚至沒有問過我在問如何讓每個元素只出現一次 –

+0

我意識到這一點。如果您只是移動該大括號,您的代碼將按照您的要求工作。 – Jashaszun

回答

0

您應該將數組保存爲字段變量,並實際刪除一個選定的條目。這樣你不會兩次相同。

package com.example.bibiwars.skills; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 
import java.util.Random; 


public class MainActivity extends ActionBarActivity { 
final ArrayList<String> places = = new ArrayList<>(Arrays.asList("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")); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ImageView img = (ImageView) findViewById(R.id.logo); 
    img.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      //ToastMessage to test the click :::::: 
      Toast.makeText(MainActivity.this, "Clicked!!",Toast.LENGTH_SHORT).show(); 
      TextView text = (TextView) findViewById(R.id.text); 
      if(places.size() >0) 
      { 
       int rnd = new Random().nextInt(places.size()); 
       text.setText(places.get(rnd)); 
       places.remove(rnd); 
      } 
     } 

    }); 
} 
//... other methods... 
} 
0

洗牌陣列

// Implementing Fisher–Yates shuffle 
    static void shuffleArray(int[] ar) 
    { 
    Random rnd = new Random(); 
    for (int i = ar.length - 1; i > 0; i--) 
    { 
     int index = rnd.nextInt(i + 1); 
     // Simple swap 
     int a = ar[index]; 
     ar[index] = ar[i]; 
     ar[i] = a; 
    } 
    } 

當您單擊顯示下一個數組元素。

+0

這是一個我喜歡,真的想在開始時,但我想刪除一個元素後,使用它...無論如何謝謝你 –