BAB 6
Struktur Kontrol
Jawaban 6.5 Latihan
6.5.1 Nilaiimport javax.swing.JOptionPane;
public class latnilai {
public static void main( String[] args ){
int nilai1;
int nilai2;
int nilai3;
nilai1 = Integer.parseInt(JOptionPane.showInputDialog("masukkan nilai pertama"));
nilai2 = Integer.parseInt(JOptionPane.showInputDialog("masukkan nilai kedua"));
nilai3 = Integer.parseInt(JOptionPane.showInputDialog("masukkan nilai ketiga"));
int rata = ((nilai1+nilai2+nilai3)/3);
if (rata >= 60) JOptionPane.showMessageDialog(null,"nilai rata-ratanya adalah" +" "+ rata +" "+":)");
else JOptionPane.showMessageDialog(null, "nilai rata-ratanya adalah" +" "+ rata +" "+":-(");
}
}
6.5.2Membaca Bilangan
import javax.swing.*;
public class angkatokata {
public static void main( String[] args ){
int angka;
angka = Integer.parseInt(JOptionPane.showInputDialog("masukkan angka"));
switch (angka) {
case 1:
JOptionPane.showMessageDialog(null, "satu");
break;
case 2:
JOptionPane.showMessageDialog(null, "dua");
break;
case 3:
JOptionPane.showMessageDialog(null, "tiga");
break;
case 4:
JOptionPane.showMessageDialog(null, "empat");
break;
case 5:
JOptionPane.showMessageDialog(null, "lima");
break;
case 6:
JOptionPane.showMessageDialog(null, "enam");
break;
case 7:
JOptionPane.showMessageDialog(null, "tujuh");
break;
case 8:
JOptionPane.showMessageDialog(null, "delapan");
break;
case 9:
JOptionPane.showMessageDialog(null, "sembilan");
break;
case 10:
JOptionPane.showMessageDialog(null, "sepuluh");
default:
JOptionPane.showMessageDialog(null, "invalid numbers");
break;
}
}
}
6.5.3 Cetak Seratus Kali
public class loopkata {
public static void main( String[] args ){
int ali=1;
while (ali<=100)
{
System.out.print("ali");
ali++;
}
}
}
6.5.4 Perpangkatan
import javax.swing.*;
public class pangkat {
public static void main( String[] args ){
int angka;
int pang;
int hasil=1;
int i;
angka = Integer.parseInt(JOptionPane.showInputDialog("masukkan angka"));
pang = Integer.parseInt(JOptionPane.showInputDialog("masukkan pangkat"));
for(i=1;i<=pang;i++){ hasil=hasil*angka; } JOptionPane.showMessageDialog(null, hasil); } }
No comments:
Post a Comment