29 Ağustos 2013 Perşembe

Java'da if Yapısı

Java if kullanımı

Aşağıdaki basit java programı klavyeden girilen değerleri parseDouble metodunu kullanarak double türüne parse edip çevirdikten sonra if komutu ile değerlendirip sonucu görüntüler. Klavyeden JOptionPane ile girilen değerler String türünde saklanır. Bu yüzden eğer bu değerlerle matematiksel işlem yapılacaksa integer, double ya da float türlerine çevrilmesi gerekir. String değerlerin sayısal değerlere çevrilmesi işlemine Java'da parse yapma işlemi denir.


import javax.swing.JOptionPane;

public class ifuygulama {
    public static void main(String args[]){
       
        double ogrenciNot;
       
        not=Double.parseDouble(JOptionPane.showInputDialog("0-100 arası not giriniz:"));
    if(ogrenciNot>=90)
    {JOptionPane.showMessageDialog(null,"A","Öğrenci notu",JOptionPane.PLAIN_MESSAGE);}
    else if(ogrenciNot>=75)
    {JOptionPane.showMessageDialog(null,"B","Öğrenci notu",JOptionPane.PLAIN_MESSAGE);}
    else if(ogrenciNot>=60)
    {JOptionPane.showMessageDialog(null,"C","Öğrenci notu",JOptionPane.PLAIN_MESSAGE);}
    else if(ogrenciNot>=50)
    {JOptionPane.showMessageDialog(null,"D","Öğrenci notu",JOptionPane.PLAIN_MESSAGE);}
    else if(ogrenciNot>=40)
    {JOptionPane.showMessageDialog(null,"E","Öğrenci notu",JOptionPane.PLAIN_MESSAGE);}
    else
    {JOptionPane.showMessageDialog(null,"F","Öğrenci notu",JOptionPane.PLAIN_MESSAGE);}
   
    }

}