Merhaba arkadaşlar bugünkü yazımızda Java dilini kullanarak basit bir hesap makinesi yapacağız.
Öncelikle kullanıcıdan işleme girecek sayılarını girmesini istiyoruz daha sonra toplama, çıkarma, çarpma ve bölme işlemlerinden birisini seçmesini istiyoruz ve sonucu ekrana yazdırıyoruz.
import java.util.Scanner;
Kodumuza başlarken kullanıcıdan veri alabilmek için Scanner sınıfı import ediyoruz.
Scanner input = new Scanner(System.in);
Kodu ile kullanıcı klavyeden veri girebiliyor.
sayi1=input.nextDouble();
Burada double kullanmamızın sebebi bölme işleminde sayıların virgüllü çıkabilecek olması. Double sayesinde virgüllü sayıları yazabiliriz.
if(islem==1)
If’ler ile kullanıcının hangi işlemi seçtiğini kontrol ediyoruz ve ona göre işlemimizi gerçekleştirip ekranı sonuca bastırıyoruz.
import</span><span class="enlighter-k10"> java.util.Scanner</span><span class="enlighter-text">;</span></div> </div> <div class=""> <div><span class="enlighter-k0">public</span> <span class="enlighter-k0">class</span><span class="enlighter-text"> Hesap_Makinesi</span></div> </div> <div class=""> <div><span class="enlighter-g1">{</span></div> </div> <div class=""> <div><span class="enlighter-k0">public</span> <span class="enlighter-k0">static</span> <span class="enlighter-k5">void</span> <span class="enlighter-m0">main</span><span class="enlighter-g1">(</span><span class="enlighter-k5">String</span><span class="enlighter-g1">[]</span><span class="enlighter-text"> args</span><span class="enlighter-g1">)</span></div> </div> <div class=""> <div><span class="enlighter-g1">{</span></div> </div> <div class=""> <div><span class="enlighter-text">Scanner input = </span><span class="enlighter-k3">new</span> <span class="enlighter-m0">Scanner</span><span class="enlighter-g1">(</span><span class="enlighter-text">System.</span><span class="enlighter-m3">in</span><span class="enlighter-g1">)</span><span class="enlighter-text">;</span></div> </div> <div class=""> <div><span class="enlighter-k5">double</span><span class="enlighter-text"> sayi1,sayi2;</span></div> </div> <div class=""> <div><span class="enlighter-k5">int</span><span class="enlighter-text"> islem;</span></div> </div> <div class=""> <div><span class="enlighter-text">System.</span><span class="enlighter-m3">out</span><span class="enlighter-text">.</span><span class="enlighter-m3">printf</span><span class="enlighter-g1">(</span><span class="enlighter-s0">" Birinci sayınızı giriniz: "</span><span class="enlighter-g1">)</span><span class="enlighter-text">;</span></div> </div> <div class=""> <div><span class="enlighter-text">sayi1=input.</span><span class="enlighter-m3">nextDouble</span><span class="enlighter-g1">()</span><span class="enlighter-text">;</span></div> </div> <div class=""> <div><span class="enlighter-text">System.</span><span class="enlighter-m3">out</span><span class="enlighter-text">.</span><span class="enlighter-m3">printf</span><span class="enlighter-g1">(</span><span class="enlighter-s0">" İkinci sayınızı giriniz: "</span><span class="enlighter-g1">)</span><span class="enlighter-text">;</span></div> </div> <div class=""> <div><span class="enlighter-text">sayi2=input.</span><span class="enlighter-m3">nextDouble</span><span class="enlighter-g1">()</span><span class="enlighter-text">;</span></div> </div> <div class=""> <div><span class="enlighter-text">System.</span><span class="enlighter-m3">out</span><span class="enlighter-text">.</span><span class="enlighter-m3">printf</span><span class="enlighter-g1">(</span><span class="enlighter-s0">" İşlem numaranızı girinizn1-Toplaman2-Çıkarman3-Çarpman4-Bölme "</span><span class="enlighter-g1">)</span><span class="enlighter-text">;</span></div> </div> <div class=""> <div><span class="enlighter-text">islem=input.</span><span class="enlighter-m3">nextInt</span><span class="enlighter-g1">()</span><span class="enlighter-text">;</span></div> </div> <div class=""> <div><span class="enlighter-k1">if</span><span class="enlighter-g1">(</span><span class="enlighter-text">islem==</span><span class="enlighter-n1">1</span><span class="enlighter-g1">)</span></div> </div> <div class=""> <div><span class="enlighter-text">System.</span><span class="enlighter-m3">out</span><span class="enlighter-text">.</span><span class="enlighter-m3">println</span><span class="enlighter-g1">(</span><span class="enlighter-s0">"Sonucunuz: "</span><span class="enlighter-text">+ </span><span class="enlighter-g1">(</span><span class="enlighter-text">sayi1+sayi2</span><span class="enlighter-g1">))</span><span class="enlighter-text">;</span></div> </div> <div class=""> <div><span class="enlighter-k1">else</span> <span class="enlighter-k1">if</span><span class="enlighter-g1">(</span><span class="enlighter-text">islem==</span><span class="enlighter-n1">2</span><span class="enlighter-g1">)</span></div> </div> <div class=""> <div><span class="enlighter-text">System.</span><span class="enlighter-m3">out</span><span class="enlighter-text">.</span><span class="enlighter-m3">println</span><span class="enlighter-g1">(</span><span class="enlighter-s0">"Sonucunuz: "</span><span class="enlighter-text">+ </span><span class="enlighter-g1">(</span><span class="enlighter-text">sayi1/sayi2</span><span class="enlighter-g1">))</span><span class="enlighter-text">;</span></div> </div> <div class=""> <div><span class="enlighter-k1">else</span> <span class="enlighter-k1">if</span><span class="enlighter-g1">(</span><span class="enlighter-text">islem==</span><span class="enlighter-n1">3</span><span class="enlighter-g1">)</span></div> </div> <div class=""> <div><span class="enlighter-text">System.</span><span class="enlighter-m3">out</span><span class="enlighter-text">.</span><span class="enlighter-m3">println</span><span class="enlighter-g1">(</span><span class="enlighter-s0">"Sonucunuz: "</span><span class="enlighter-text">+ </span><span class="enlighter-g1">(</span><span class="enlighter-text">sayi1*sayi2</span><span class="enlighter-g1">))</span><span class="enlighter-text">;</span></div> </div> <div class=""> <div><span class="enlighter-k1">else</span> <span class="enlighter-k1">if</span><span class="enlighter-g1">(</span><span class="enlighter-text">islem==</span><span class="enlighter-n1">4</span><span class="enlighter-g1">)</span></div> </div> <div class=""> <div><span class="enlighter-text">System.</span><span class="enlighter-m3">out</span><span class="enlighter-text">.</span><span class="enlighter-m3">println</span><span class="enlighter-g1">(</span><span class="enlighter-s0">"Sonucunuz: "</span><span class="enlighter-text">+ </span><span class="enlighter-g1">(</span><span class="enlighter-text">sayi1/sayi2</span><span class="enlighter-g1">))</span><span class="enlighter-text">;</span></div> </div> <div class=""> <div><span class="enlighter-k1">else</span></div> </div> <div class=""> <div><span class="enlighter-text">System.</span><span class="enlighter-m3">out</span><span class="enlighter-text">.</span><span class="enlighter-m3">printf</span><span class="enlighter-g1">(</span><span class="enlighter-s0">"Hatalı işlem girdiniz"</span><span class="enlighter-g1">)</span><span class="enlighter-text">;</span></div> </div> <div class=""> <div><span class="enlighter-g1">}</span></div> </div> <div class=""> <div><span class="enlighter-g1">}