Posted by Materi Teknik Informatika • Informasi Teknologi Informasi • Digital Marketing on 10/26/2014
Kondisional Switch bersarang terjadi dengan adanya Switch yang didalamnya terdapat Case, lalu terdapat lagi Switch dalam Case sebelumnya yang terdapat Switch Case sampai seterusnya.
Berikut ini Source Code Program Nested Switch dalam kasus (Program Menginputkan Tanggal)
package Nested;
import java.util.Scanner;
public class Nested_Switch {
public static void main(String[] args) {
Scanner masukan = new Scanner(System.in);
System.out.print("All Rights Reserved - Septian Chun Chan\n");
System.out.print("Hari ke - :");
int hari = masukan.nextInt();
System.out.print("Bulan ke - :");
int bulan = masukan.nextInt();
System.out.print("Tahun ke - :");
int tahun = masukan.nextInt();
System.out.print("Tanggal Sekarang :\n");
switch (hari) {
case 1: System.out.println("Senin");
switch (bulan) {
case 1: System.out.println("Januari"); break;
case 2: System.out.println("Februari"); break;
case 3: System.out.println("Maret"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("Mei"); break;
case 6: System.out.println("Juni"); break;
case 7: System.out.println("Juli"); break;
case 8: System.out.println("Agustus"); break;
case 9: System.out.println("September"); break;
case 10: System.out.println("Oktober"); break;
case 11: System.out.println("November"); break;
case 12: System.out.println("Desember"); break;
}
break;
case 2: System.out.println("Selasa");
switch (bulan) {
case 1: System.out.println("Januari"); break;
case 2: System.out.println("Februari"); break;
case 3: System.out.println("Maret"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("Mei"); break;
case 6: System.out.println("Juni"); break;
case 7: System.out.println("Juli"); break;
case 8: System.out.println("Agustus"); break;
case 9: System.out.println("September"); break;
case 10: System.out.println("Oktober"); break;
case 11: System.out.println("November"); break;
case 12: System.out.println("Desember"); break;
}
break;
case 3: System.out.println("Rabu");
switch (bulan) {
case 1: System.out.println("Januari"); break;
case 2: System.out.println("Februari"); break;
case 3: System.out.println("Maret"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("Mei"); break;
case 6: System.out.println("Juni"); break;
case 7: System.out.println("Juli"); break;
case 8: System.out.println("Agustus"); break;
case 9: System.out.println("September"); break;
case 10: System.out.println("Oktober"); break;
case 11: System.out.println("November"); break;
case 12: System.out.println("Desember"); break;
}
break;
case 4: System.out.println("Kamis");
switch (bulan) {
case 1: System.out.println("Januari"); break;
case 2: System.out.println("Februari"); break;
case 3: System.out.println("Maret"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("Mei"); break;
case 6: System.out.println("Juni"); break;
case 7: System.out.println("Juli"); break;
case 8: System.out.println("Agustus"); break;
case 9: System.out.println("September"); break;
case 10: System.out.println("Oktober"); break;
case 11: System.out.println("November"); break;
case 12: System.out.println("Desember"); break;
}
break;
case 5: System.out.println("Jumat");
switch (bulan) {
case 1: System.out.println("Januari"); break;
case 2: System.out.println("Februari"); break;
case 3: System.out.println("Maret"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("Mei"); break;
case 6: System.out.println("Juni"); break;
case 7: System.out.println("Juli"); break;
case 8: System.out.println("Agustus"); break;
case 9: System.out.println("September"); break;
case 10: System.out.println("Oktober"); break;
case 11: System.out.println("November"); break;
case 12: System.out.println("Desember"); break;
}
break;
case 6: System.out.println("Sabtu");
switch (bulan) {
case 1: System.out.println("Januari"); break;
case 2: System.out.println("Februari"); break;
case 3: System.out.println("Maret"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("Mei"); break;
case 6: System.out.println("Juni"); break;
case 7: System.out.println("Juli"); break;
case 8: System.out.println("Agustus"); break;
case 9: System.out.println("September"); break;
case 10: System.out.println("Oktober"); break;
case 11: System.out.println("November"); break;
case 12: System.out.println("Desember"); break;
}
break;
case 7: System.out.println("Minggu");
switch (bulan) {
case 1: System.out.println("Januari"); break;
case 2: System.out.println("Februari"); break;
case 3: System.out.println("Maret"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("Mei"); break;
case 6: System.out.println("Juni"); break;
case 7: System.out.println("Juli"); break;
case 8: System.out.println("Agustus"); break;
case 9: System.out.println("September"); break;
case 10: System.out.println("Oktober"); break;
case 11: System.out.println("November"); break;
case 12: System.out.println("Desember"); break;
}
break;
}
switch (tahun) {
case 1: System.out.println("2001"); break;
case 2: System.out.println("2002"); break;
case 3: System.out.println("2003"); break;
case 4: System.out.println("2004"); break;
case 5: System.out.println("2005"); break;
case 6: System.out.println("2006"); break;
case 7: System.out.println("2007"); break;
case 8: System.out.println("2008"); break;
case 9: System.out.println("2009"); break;
case 10: System.out.println("2010"); break;
case 11: System.out.println("2011"); break;
case 12: System.out.println("2012"); break;
case 13: System.out.println("2013"); break;
case 14: System.out.println("2014"); break;
}
}
}
Berikut ini tampilan output Program Menginputkan Tanggal dengan Nested Switch