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

안드로이드(자바) 홀/짝 맞추기 예제

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 MainActivity5 extends AppCompatActivity {


	//이렇게 선언할 수 있음
    EditText etMe, etCom, etResult;

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

        etMe = findViewById(R.id.et_mine);
        etCom = findViewById(R.id.et_com);
        etResult = findViewById(R.id.et_result);

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

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

        public void myClick(){
            Random random = new Random();
            int n =random.nextInt(2);
            String str = "";
            if (n==0){
                str = "짝";
                etCom.setText(str);
            }else{
                str = "홀";
                etCom.setText(str);
            }
            int mine = Integer.parseInt(String.valueOf(etMe.getText()));
            if ((mine%2==0 && str.equals("짝")) || (mine%2==1 && str.equals("홀"))){
                etResult.setText("승리!");
            }else{
                etResult.setText("패배!");
            }
        }
}

 

 

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="나"
                android:textSize="30dp" />

            <EditText
                android:id="@+id/et_mine"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="number"
                android:textSize="20dp" />


        </LinearLayout>

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="컴"
                android:textSize="30dp" />

            <EditText
                android:id="@+id/et_com"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="number"
                android:textSize="20dp" />

        </LinearLayout>

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="결과"
                android:textSize="30dp" />

            <EditText
                android:id="@+id/et_result"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="text"
                android:textSize="20dp" />


        </LinearLayout>

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="게임하기" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

결과