본문 바로가기
안드로이드/안드로이드 연습

안드로이드(자바) 로또 출력 예제

by 미눅스[멘토] 2023. 6. 27.
728x90

 

 

package kr.co.aiai.app;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Random;

//로또만들기
public class MainActivity4 extends AppCompatActivity {


    EditText et1;
    EditText et2;
    EditText et3;
    EditText et4;
    EditText et5;
    EditText et6;


    EditText et;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);

       et1 = findViewById(R.id.et1);
       et2 = findViewById(R.id.et2);
       et3 = findViewById(R.id.et3);
       et4 = findViewById(R.id.et4);
       et5 = findViewById(R.id.et5);
       et6 = findViewById(R.id.et6);

       Button btn = findViewById(R.id.btn);

       btn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {

               myClick();

           }
       });


    }
    int[] num = new int[45];
    int r=0;
    public void myClick(){
        Random random = new Random();
        for ( int i=0; i<num.length; i++){
            num[i] = i+1;
        }

        for ( int i=1; i<=100; i++){
            int randomIndex = random.nextInt(45);
            //int rnd = (int )(Math.random()*45);//Math.random이걸로 많이써야 C언어가서도 좋다
            r = num[0];
            num[0] = num[randomIndex];
            num[randomIndex] = r;
        }

        et1.setText(Integer.toString(num[0]));
        et2.setText(Integer.toString(num[1]));
        et3.setText(Integer.toString(num[2]));
        et4.setText(Integer.toString(num[3]));
        et5.setText(Integer.toString(num[4]));
        et6.setText(Integer.toString(num[5]));


        Log.d("minwoo",Integer.toString(num[1]));

    }
}

 

 

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:ignore="MissingConstraints">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/et1"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:text="__" />
            <EditText
                android:id="@+id/et2"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:text="__" />

            <EditText
                android:id="@+id/et3"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:text="__" />

            <EditText
                android:id="@+id/et4"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:text="__" />

            <EditText
                android:id="@+id/et5"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:text="__" />

            <EditText
                android:id="@+id/et6"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:text="__" />


        </LinearLayout>

        <Button
            android:id="@+id/btn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="로또생성하기" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" >

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApp"
        tools:targetApi="31" >
        <activity
            android:name=".MainActivity4"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
    </application>

</manifest>

결과