IF(kondisi){
Statement1
}else{
Statement 2
};
sedangkan Switch meliki aturan penulisan seperti berikut:
switch(variable){
case 1:
Statement1
break;
default:
Statement2
break;
};
Oke langsung saja kita meluncur ke programnya seperti biasa buka Editor java saudara kemudian
ketikan program berikut
Metode penulisan IF
import javax.swing.JOptionPane;
String input = JOptionPane.showInputDialog("Masukkan Angka 1-10 ");
int hasil = Integer.parseInt(input);
if(hasil==1){
JOptionPane.showMessageDialog(null, "Hallo");
}else if(hasil==2){
JOptionPane.showMessageDialog(null, "Welcome");
}else if(hasil==3){
JOptionPane.showMessageDialog(null, "Message");
}else if(hasil==4){
JOptionPane.showMessageDialog(null, "I'am Busy");
}else if (hasil==5){
JOptionPane.showMessageDialog(null, "Machrus' Here");
}else if(hasil==6){
JOptionPane.showMessageDialog(null, "Call me Again");
}else if(hasil==7){
JOptionPane.showMessageDialog(null, "Don't worry");
}else if(hasil==8){
JOptionPane.showMessageDialog(null, "I'am Working");
}else if(hasil==9){
JOptionPane.showMessageDialog(null, "Thank's For calling");
}else if(hasil==10){
JOptionPane.showMessageDialog(null, "I Love U");
}else{
JOptionPane.showMessageDialog(null, "I Need U")
}
Metode Penulisan Switch
import javax.swing.JOptionPane;
String input = JOptionPane.showInputDialog("Masukkan Angka 1-10 ");
int hasil = Integer.parseInt(input);
switch(hasil){
case 1:
JOptionPane.showMessageDialog(null, "Hallo");
break;
case 2:
JOptionPane.showMessageDialog(null, "Welcome");
break;
case 3:
JOptionPane.showMessageDialog(null, "Message");
break;
case 4:
JOptionPane.showMessageDialog(null, "I'am Busy");
break;
case 5:
JOptionPane.showMessageDialog(null, "Machrus' Here");
break;
case 6:
JOptionPane.showMessageDialog(null, "Call me Again");
break;
case 7:
JOptionPane.showMessageDialog(null, "Don't worry");
break;
case 8:
JOptionPane.showMessageDialog(null, "I'am Working");
break;
case 9:
JOptionPane.showMessageDialog(null, "Thank's For calling");
break;
case 10:
JOptionPane.showMessageDialog(null, "I Love U");
break;
default:
JOptionPane.showMessageDialog(null, "I Need U");
break;
}
Nah kemudian coba Compile program saudara dan Run dan akan menghasilkan program seperti berikut :
sekian postingan mengenasi Struktur Kontrol IF dan Switch menggunakan JOptionPane Terima kasih dan semoga bermanfaat
Download Project IF Else
Download Project Switch
0 komentar:
Posting Komentar