dimanche 25 octobre 2015

Random generating in Android

I want to make an app of Lucky Draw of seven numbers.When you click the "next number" button each time, it generates a random number. When generating the last number, the app will do the sorting except the last number. After generating the last number, click the button will have no response. You need to click "clear" button to restart. And the numbers cannot repeat. I have tries so many times and it still cannot work. Usually, the app is stopped or cannot generate the numbers one by one.

MainActivity.java

public void generate (View v){
Random myRandom = new Random();
int i = 0;
int[] Arr = new int[5] ;
int number_7 = 0;
TextView tv_number_one = (TextView)findViewById(R.id.tv_number_1);
TextView tv_number_two = (TextView)findViewById(R.id.tv_number_2);
TextView tv_number_three = (TextView)findViewById(R.id.tv_number_3);
TextView tv_number_four = (TextView)findViewById(R.id.tv_number_4);
TextView tv_number_five = (TextView)findViewById(R.id.tv_number_5);
TextView tv_number_six = (TextView)findViewById(R.id.tv_number_6);
TextView tv_number_seven = (TextView)findViewById(R.id.tv_number_7);

if(i<=6){
    int num = myRandom.nextInt(48) +1;
    Arr[i] = num;
    i++;
}else if(i == 6){
    int num_7 = myRandom.nextInt(48)+1;
    number_7 = num_7;
    i++;
}
tv_number_one.setText("" + String.valueOf(Arr[0]));
tv_number_two.setText("" + String.valueOf(Arr[1]));
tv_number_three.setText("" + String.valueOf(Arr[2]));
tv_number_four.setText("" + String.valueOf(Arr[3]));
tv_number_five.setText("" + String.valueOf(Arr[4]));
tv_number_six.setText("" + String.valueOf(Arr[5]));
tv_number_seven.setText("" + String.valueOf(number_7));
}




public void clear (View v){
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("?");
tv_number_two.setText("?");
tv_number_three.setText("?");
tv_number_four.setText("?");
tv_number_five.setText("?");
tv_number_six.setText("?");
tv_number_seven.setText("?");

}

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"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CLEAR"
    android:id="@+id/button_clear"
    android:onClick="clear"
    android:layout_alignTop="@+id/button_next_num"
    android:layout_toRightOf="@+id/tv_number_six"
    android:layout_toEndOf="@+id/tv_number_six"
    android:layout_marginLeft="30dp" />




Aucun commentaire:

Enregistrer un commentaire