0

I want to make an app (just for learning purposes) that generates a random message without repeatition.

I have this code inside the onCreate method:

imgbutton2 = (ImageButton) findViewById(R.id.imgbutton2);

int n=3;
int v1[] = new int[n];
v1[0] = 0;
v1[1] = 0;
v1[2] = 0;

int i, j;

for(i=0;i<n;i++){
    for(j=0; ;j++){
        Random rn = new Random();
        int range = 3;
        int r = rn.nextInt(range) 1;

        if(r != v1[0] & r != v1[1] & r != v1[2]) {
            v1[i] = r v1[i];

            textView1=new TextView(this); 

            switch (r) {
                case 1:
                    textView1 = (TextView) findViewById(R.id.textView1); 
                    textView1.setText("Message 1");
                break;

                case 2:
                    textView1 = (TextView) findViewById(R.id.textView1); 
                    textView1.setText("Message 2");
                break;

                case 3:
                    textView1 = (TextView) findViewById(R.id.textView1); 
                    textView1.setText("Message 3");
                break;
            }
            break;
        }
    }

    imgbutton2.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Intent i = null;
        }});
    }

I put a null function to the imgbutton2 (that's inside the first FOR), just for wait the user click, and continue reading to enter again in the first FOR with the i=1 now), but it's not working.

The idea is generate a new message when the button is clicked.

Can anyone help me?

TtT23
  • 6,876
  • 34
  • 103
  • 174
  • It doesn't seem like you are actually doing anything with the onClick event – TtT23 Feb 28 '13 at 05:25
  • Your code is unformatted and hard to read, and seems to be missing several } characters. I'm also not quite sure what's actually happening and what you want to happen. Care to edit and clarify your question? – Gabe Sechan Feb 28 '13 at 05:26
  • And how can I fix it? I just want to make a pause in the 'for', and generate a new message after clicking the button –  Feb 28 '13 at 05:28
  • @l46kok is right nothing is actually done on onclick – Shiv Feb 28 '13 at 05:28
  • You want to generate a new message when the button is clicked.. yet you aren't actually doing anything with the onClick event for the button??? – TtT23 Feb 28 '13 at 05:32
  • whats the range of your randon no i mean no of digits ? 3 or 4 or 5 ? – itsrajesh4uguys Feb 28 '13 at 05:42
  • my idea was make a null button, and after it's clicked, the 'for' proceed with the loop until i=3, right? i knew this was wrong, but i wanna know what i should put in the onClick event to make the 'for' start again with i=1; –  Feb 28 '13 at 05:44

1 Answers1

0

Android provides the API to get the Random Number: Use this on your button click event and get random number with no repetition.

 Random myRandom = new Random();
 int randomNumber = myRandom.nextInt();
 Log.i("RandomNumber:" + randomNumber);
Hulk
  • 2,565
  • 16
  • 24