Skip to main content
1 de 3

Números aleatórios no Android Studio

Esse código está gerando números aleatórios em 6 TextViews diferentes, mas não consigo fazer um código pra que evite que esses números se repitam.`package com.example.kelvin.myapplication;

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; import android.view.; import android.content.; import android.widget.EditText; import android.widget.TextView; import java.util.Random;

public class MainActivity extends AppCompatActivity {

public Button btnsim;
public TextView texto1;
public TextView texto2;
public TextView Texto3;
public TextView Texto4;
public TextView Texto5;
public TextView Texto6;


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

public void exibir(View view){

            Random myRandom = new Random();
            Random myRandom2 = new Random();
            Random myRandom3 = new Random();
            Random myRandom4 = new Random();
            Random myRandom5 = new Random();
            Random myRandom6 = new Random();

            int number = myRandom.nextInt(60) + 1;
            int number2 = myRandom2.nextInt(60) + 1;
            int number3 = myRandom3.nextInt(60) + 1;
            int number4 = myRandom4.nextInt(60) + 1;
            int number5 = myRandom5.nextInt(60) + 1;
            int number6 = myRandom6.nextInt(60) + 1;

            TextView mytext = (TextView) findViewById(R.id.texto1);
            TextView mytext2 = (TextView) findViewById(R.id.texto2);
            TextView mytext3 = (TextView) findViewById(R.id.texto3);
            TextView mytext4 = (TextView) findViewById(R.id.texto4);
            TextView mytext5 = (TextView) findViewById(R.id.texto5);
            TextView mytext6 = (TextView) findViewById(R.id.texto6);

            String myString = String.valueOf(number);
            String myString2 = String.valueOf(number2);
            String myString3 = String.valueOf(number3);
            String myString4 = String.valueOf(number4);
            String myString5 = String.valueOf(number5);
            String myString6 = String.valueOf(number6);

            mytext.setText(myString);
            mytext2.setText(myString2);
            mytext3.setText(myString3);
            mytext4.setText(myString4);
            mytext5.setText(myString5);
            mytext6.setText(myString6);
    }

public void naoexibir (View view) {

    Intent intent = new Intent(this, Main2Activity.class);
    startActivity(intent);

}

} `