TERJEMAHAN ANGKA MENGGUNAKAN JAVA

Posted on Senin, 10 Januari 2011 by muceiimutia

import javax.swing.JOptionPane;
/**
 *
 * @author */
public class array2 {
public static void main(String[] args) {
        int x;
        String nilai = "";

        do {
            try {
                x = Integer.parseInt(JOptionPane.showInputDialog("Masukkan angka (0-999)"));
                if (x >= 0 && x <= 999) {
                    String[] angka = { //array angka dasar dan angka tertentu
                        "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen"
                    };
                    String[] puluhan = { //array untuk angka puluhan dari 20 - 90
                        "twenty", "thirty", "fourty", "fivty", "sixty", "seventy", "eighty", "ninety"
                    };
                    if (x == 0) {// apabila x = 0 maka muncul "zero"
                        nilai = angka[0];

                    } else if (x > 0 && x <= 15) {// apabila x>0 dan x<=15 maka muncul array yang sudah di tentukan
                        nilai = angka[x];

                    } else if (x > 15 && x < 100) {
                        int mod = x % 10;
                        int div = x / 10;
                        if (mod == 0) {
                            nilai = puluhan[div - 2];
                        } else {
                            if (x < 20) {
                                x -= 10;
                                if (x==8) { // angka 8 dibedakan karena 8 adalah khusus jika di inggriskan
                                    nilai = angka[x] + "een";
                                }
                                else {
                                    nilai = angka[x] + "teen";
                                }
                            } else {
                                    nilai = puluhan[div - 2] + " " + angka[mod];
                            }
                        }
                    } else if (x > 99) {
                        String rts = " hundred ";
                        int mod = x % 100;
                        int div = x / 100;
                        if (mod == 0) {
                            nilai = angka[div] + rts;
                        } else if (mod > 0 && mod < 16) {
                            nilai = angka[div] + rts + angka[mod];
                        } else if (mod > 15 && mod < 20) {
                            if (mod==18) {
                                nilai = angka[div] + rts + angka[mod % 10] + "een";
                            }
                            else {
                                nilai = angka[div] + rts + angka[mod % 10] + "teen";
                            }
                        } else {
                            if (mod % 10 == 0) {
                                nilai = angka[div] + rts + puluhan[(mod / 10) - 2];
                            } else {
                                nilai = angka[div] + rts + puluhan[(mod / 10) - 2] + " " + angka[mod % 10];
                            }
                        }
                    } JOptionPane.showMessageDialog(null, "INGGRIS " + x + " : " + nilai); //
                } else {
                    JOptionPane.showMessageDialog(null, "angka tidak terbaca", "ERROR", JOptionPane.ERROR_MESSAGE);
                }

            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "bukan bilangan bulat", "ERROR", JOptionPane.ERROR_MESSAGE);
            }
        } while (JOptionPane.showConfirmDialog(null, "ulangi?", "created by MUTIA", JOptionPane.ERROR_MESSAGE) == JOptionPane.YES_NO_OPTION);
    }
}

0 Responses to "TERJEMAHAN ANGKA MENGGUNAKAN JAVA":