• 7.02.2025
  • 55
  • Java ile Hesap Makinası Android Studio | Makale

JAVA İLE HESAP MAKİNASI ANDROİD STUDİO

xml kodları

 

< 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="16dp"
    android:gravity="center">

    <EditText
        android:id="@+id/num1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Birinci Sayı"
        android:inputType="numberDecimal" />

    <EditText
        android:id="@+id/num2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="İkinci Sayı"
        android:inputType="numberDecimal" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_marginTop="16dp">

        <Button
            android:id="@+id/btnTopla"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="+" />

        <Button
            android:id="@+id/btnCikar"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="-" />

        <Button
            android:id="@+id/btnCarp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="×" / >

<Button android:id="
@+id/btnBol" android:layout_height="wrap_content"
 android:layout_weight="1"
 android:layout_width="0dp"
 android:text="÷" />

 

</LinearLayout >
<TextView
        android:id="@+id/txtSonuc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Sonuç: "
        android:textSize="20sp"
        android:gravity="center"
        android:layout_marginTop="20dp" />

 

 

java kodları

 


	package com.example.hesapmakinesi;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    EditText num1, num2;
    Button btnTopla, btnCikar, btnCarp, btnBol;
    TextView txtSonuc;

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

        num1 = findViewById(R.id.num1);
        num2 = findViewById(R.id.num2);
        btnTopla = findViewById(R.id.btnTopla);
        btnCikar = findViewById(R.id.btnCikar);
        btnCarp = findViewById(R.id.btnCarp);
        btnBol = findViewById(R.id.btnBol);
        txtSonuc = findViewById(R.id.txtSonuc);

        btnTopla.setOnClickListener(view -> hesapla('+'));
        btnCikar.setOnClickListener(view -> hesapla('-'));
        btnCarp.setOnClickListener(view -> hesapla('*'));
        btnBol.setOnClickListener(view -> hesapla('/'));
    }

    private void hesapla(char islem) {
        String s1 = num1.getText().toString();
        String s2 = num2.getText().toString();

        if (s1.isEmpty() || s2.isEmpty()) {
            txtSonuc.setText("Lütfen iki sayı giriniz!");
            return;
        }

        double sayi1 = Double.parseDouble(s1);
        double sayi2 = Double.parseDouble(s2);
        double sonuc = 0;

        switch (islem) {
            case '+': sonuc = sayi1 + sayi2; break;
            case '-': sonuc = sayi1 - sayi2; break;
            case '*': sonuc = sayi1 * sayi2; break;
            case '/':
                if (sayi2 == 0) {
                    txtSonuc.setText("Hata: Sıfıra bölme!");
                    return;
                }
                sonuc = sayi1 / sayi2;
                break;
        }

        txtSonuc.setText("Sonuç: " + sonuc);
    }
}

 

 

YORUM YAP