MainActivity5.java
package kr.co.aiai.app;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity5 extends AppCompatActivity {
EditText mine;
EditText com;
EditText result;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
mine = findViewById(R.id.et_mine);
com = findViewById(R.id.et_com);
result = findViewById(R.id.et_result);
btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
playGame();
}
});
}
public void playGame(){
String m = mine.getText().toString();
String c = "";
String res = "";
double rnd = Math.random();
if(rnd>0.5){
c = "홀";
} else{
c = "짝";
}
if(c.equals(m)){
res = "승리";
}else{
res = "패배";
}
com.setText(c);
result.setText(res);
}
}
activity_main5.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
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="text"
android:textSize="20dp" />
</LinearLayout>
<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_com"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"
android:textSize="20dp" />
</LinearLayout>
<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_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number"
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>

'AndroidStudio' 카테고리의 다른 글
| [Android Studio] 별 찍기 (0) | 2023.06.28 |
|---|---|
| [Android Studio] 구구단 출력하기 (0) | 2023.06.28 |
| [Android Studio] 로또 생성하기 (0) | 2023.06.28 |
| [Android Studio] A부터 B까지 합 C 구하기 (0) | 2023.06.27 |
| [Android Studio] 버튼 클릭시마다 10씩 증가하기 (0) | 2023.06.27 |