Minggu, 23 Juni 2013

Bahasa Pemrograman Java

Contoh Listing Program Menghitung Jumlah Hari Dalam Sebulan Dengan Bahasa Pemrograman Java

Dan Algoritma menggunakan Case



import java.util.Scanner;
public class Moon {
      public static void main(String[] args) {
        // TODO code application logic here
        String Bulan[]={"januari",
   "Februari","Maret","April","Mei","Juni","Juli",
         "Agustus", "September","Oktober", "November","Desember"};
        int NumHari,dBulan,Year;
             
        Scanner sc= new Scanner(System.in);
        System.out.print("Month Code : (1-12) ");dBulan=sc.nextInt();
            switch (dBulan) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12: NumHari=31;
  System.out.println("Month name : "+Bulan[dBulan-1]);
  break;
            case 4:
            case 6:
            case 9:
            case 11: NumHari=30;
System.out.println("Month name : "+Bulan[dBulan-1]);
break;
            case 2: 
                    System.out.print("Year: ");Year=sc.nextInt();
                    if (Year % 4 == 0)NumHari=29;
   else NumHari=28;
                    System.out.println("Month name : "+Bulan[dBulan-1]);
                    break;
          default: System.out.println("Kode bulan salah ");
       } 
        System.out.println("Number of days = "+NumHari);
        System.out.println("Finished ");
      
      }
}