본문 바로가기
App/Android Studio

Android Studio - 로또 번호 발생기

by 코젼 2022. 8. 2.
728x90
반응형

2022-08-02(31일차)


📃로또 번호 발생기

◾ 1~45까지의 5개의 로또 번호를 발생시키고, 중복되지 않게 하시오.


📂파일

lotto_create.zip
10.67MB


📌 실행 결과


📌 소스 코드

📝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
반응형

댓글