728x90
반응형
📃로또 번호 발생기
◾ 1~45까지의 5개의 로또 번호를 발생시키고, 중복되지 않게 하시오.
📂파일
📌 실행 결과
📌 소스 코드
📝MainActivity.java
package com.example.lotto_create;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lotto);
TextView lotto1 = findViewById(R.id.lotto_01);
TextView lotto2 = findViewById(R.id.lotto_02);
TextView lotto3 = findViewById(R.id.lotto_03);
TextView lotto4 = findViewById(R.id.lotto_04);
TextView lotto5 = findViewById(R.id.lotto_05);
String num[] = new String[5];
for(int i=0; i<5; i++) {
num[i] = String.valueOf((int)(Math.random()*45) + 1);
for(int j=0; j<i; j++) {
if(num[i] == num[j]) {
i--;
}
}
}
lotto1.setText(num[0]);
lotto2.setText(num[1]);
lotto3.setText(num[2]);
lotto4.setText(num[3]);
lotto5.setText(num[4]);
}
}
📝lotto.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="40dp">
<TextView
android:layout_width="300dp"
android:layout_height="50dp"
android:background="@drawable/background_round"
android:gravity="center"
android:text="오늘의 로또 번호"
android:textSize="35dp" />
<TextView
android:id="@+id/lotto_01"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:background="@drawable/round1"
android:gravity="center"
android:text="num"
android:textSize="20dp" />
<TextView
android:id="@+id/lotto_02"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/round2"
android:gravity="center"
android:text="num"
android:textSize="20dp" />
<TextView
android:id="@+id/lotto_03"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/round3"
android:gravity="center"
android:text="num"
android:textSize="20dp" />
<TextView
android:id="@+id/lotto_04"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/round4"
android:gravity="center"
android:text="num"
android:textSize="20dp" />
<TextView
android:id="@+id/lotto_05"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/round5"
android:gravity="center"
android:text="num"
android:textSize="20dp" />
</LinearLayout>
728x90
반응형
'App > Android Studio' 카테고리의 다른 글
Android Studio - OnClickListener 활용 (0) | 2022.08.03 |
---|---|
Android Studio - OnClickListener 이벤트 리스너 (0) | 2022.08.03 |
Android Studio - 메시지 출력[Log, Toast] (0) | 2022.08.02 |
Android Studio - 구구단 레이아웃 만들기 (0) | 2022.08.02 |
Android Studio - GridLayout (0) | 2022.08.02 |
댓글