Android Application

Kotlin if/else 문

작성자 임베디드코리아 작성일23-08-27 19:02 조회1,817회 댓글0건

첨부파일

----------------------------------------------------------------------------------------
    MainActivity..kt
----------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //텍스트뷰 초기화
        var textView : TextView = findViewById(R.id.textView);

        textView.append("-------------------\n")
        var number : Int = 3;
        var number2 : Int = 2;

        //조건 1개
        if(number == 3){
            textView.append("$number 는  3과 같습니다.\n");
        }
        textView.append("-------------------\n")
        //조건 2개
        if( number2 != 3){
            textView.append("$number2 는 3 과 다릅니다.\n")

        }else{
            textView.append("$number2 는 3과 같습니다.\n")
        }
        textView.append("-------------------\n")
        //val job = "Backend Developer"
        val job = "Frontend Developer"
        //val job = "Embedded Developer"

        if (job == "Backend Developer") {
            textView.append("백엔드 개발자 \n")
        } else if (job == "Frontend Developer") {
            textView.append("프론트엔드 개발자 \n")
        } else {
            textView.append("개발자 아님 \n")
        }
        textView.append("-------------------\n")
        //다중 조건
        val score = 85

        if (score >= 90) {
            textView.append("A 학점 입니다.\n")
        } else if (score >= 80) {
            textView.append("B 학점 입니다.\n")
        } else if (score >= 70) {
            textView.append("C 학점 입니다.\n")
        } else if (score >= 60) {
            textView.append("D 학점 입니다.\n")
        } else {
            textView.append("F 학점 입니다.\n")
        }
        textView.append("-------------------\n")
    }
}

----------------------------------------------------------------------------------------
    activity_main.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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>