samedi 24 octobre 2015

Android random number problems

Here is my code. Its result is 7 random numbers are generated at the same time. How can I change to generate a random number when each time the user click the "NEXT NUMBER" button. When it generates the seventh number, click the button will no response. The numbers cannot repeated and finally the first number to the sixth number will be sorted from the smallest number to the largest number.

MainActivity.java

    public void generate(View v){
    Random myRandom = new Random();

    TextView tv_number_one = (TextView)findViewById(R.id.tv_number_one);
    TextView tv_number_two = (TextView)findViewById(R.id.tv_number_two);
    TextView tv_number_three = (TextView)findViewById(R.id.tv_number_three);
    TextView tv_number_four = (TextView)findViewById(R.id.tv_number_four);
    TextView tv_number_five = (TextView)findViewById(R.id.tv_number_five);
    TextView tv_number_six = (TextView)findViewById(R.id.tv_number_six);
    TextView tv_number_seven = (TextView)findViewById(R.id.tv_number_seven);

    tv_number_one.setText(String.valueOf(myRandom.nextInt(48)));
    tv_number_two.setText(String.valueOf(myRandom.nextInt(48)));
    tv_number_three.setText(String.valueOf(myRandom.nextInt(48)));
    tv_number_four.setText(String.valueOf(myRandom.nextInt(48)));
    tv_number_five.setText(String.valueOf(myRandom.nextInt(48)));
    tv_number_six.setText(String.valueOf(myRandom.nextInt(48)));
    tv_number_seven.setText(String.valueOf(myRandom.nextInt(48)));
    Count++;




}}

activity_main

<TextView android:text="\?" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_number_one"
    android:textSize="30sp"
    android:textIsSelectable="true"
    android:layout_marginLeft="10dp"
    android:textColor="#67ceff"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_two"
    android:layout_alignBottom="@+id/tv_number_one"
    android:layout_toRightOf="@+id/tv_number_one"
    android:layout_toEndOf="@+id/tv_number_one"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_three"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/tv_number_two"
    android:layout_toEndOf="@+id/tv_number_two"
    android:layout_marginLeft="20dp"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_four"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/tv_number_three"
    android:layout_toEndOf="@+id/tv_number_three"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:textIsSelectable="true"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_five"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/tv_number_four"
    android:layout_toEndOf="@+id/tv_number_four"
    android:layout_marginLeft="20dp"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_six"
    android:layout_alignBottom="@+id/tv_number_five"
    android:layout_toRightOf="@+id/tv_number_five"
    android:layout_toEndOf="@+id/tv_number_five"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_seven"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/tv_number_six"
    android:layout_toEndOf="@+id/tv_number_six"
    android:layout_marginLeft="20dp"
    android:textColor="#6198ff"
    android:textSize="30sp"
    android:layout_marginStart="20dp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="NEXT NUMBER"
    android:id="@+id/button_next_num"
    android:layout_below="@+id/tv_number_one"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="41dp"
    android:onClick="generate"/>




Aucun commentaire:

Enregistrer un commentaire