Java Switch Case

import java.io.*;
public class pert4{
public static void main (String[]args){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
String pilH=””;
String pilH2=””;
double alas=0;
double tinggi=0;
double Lsegitiga=0;
double a=0;
double b=0;
double c=0;
double Ksegitiga=0;
double jari=0;
double Lling=0;
double diameter=0;
double Kling=0;

do{
System.out.println(“==============================”);
System.out.println(“Menghitung bangun datar”);
System.out.println(“==============================”);
System.out.println(“1. Bangun segitiga”);
System.out.println(“2. Bangun Lingkaran”);
System.out.println(“3. Lihat hasil Bangun segitiga”);
System.out.println(“4. Lihat hasil Bangun segitiga”);
System.out.println(“5. Keluar”);
System.out.print(“Masukkan pilihan : “);
int pilih = Integer.parseInt(br.readLine());
System.out.println(“”);

switch(pilih){
case 1 :
System.out.println(“==============================”);
System.out.println(“Bangun segitiga”);
System.out.println(“==============================”);
System.out.println(“Menghitung Luas atau Keliling”);
System.out.print(“Pilihan hitung : “);
pilH = br.readLine();

if(pilH.equalsIgnoreCase(“Luas”)){
System.out.println(“Luas “);
System.out.print(“Inputkan alas : “);
alas = Double.parseDouble(br.readLine());
System.out.print(“Inputkan Tinggi : “);
tinggi = Double.parseDouble(br.readLine());
Lsegitiga = alas*tinggi/2;
}
else{
System.out.println(“Keliling “);
System.out.print(“Inputkan a : “);
a = Double.parseDouble(br.readLine());
System.out.print(“Inputkan b : “);
b = Double.parseDouble(br.readLine());
System.out.print(“Inputkan c : “);
c = Double.parseDouble(br.readLine());
Ksegitiga = a+b+c;
}
break;
case 2 :
System.out.println(“==============================”);
System.out.println(“Bangun Lingkaran”);
System.out.println(“==============================”);
System.out.println(“Menghitung Luas atau Keliling”);
System.out.print(“Pilihan hitung : “);
pilH2 = br.readLine();

if(pilH2.equalsIgnoreCase(“Luas”)){
System.out.println(“Luas “);
System.out.print(“Inputkan Jari – jari : “);
jari = Double.parseDouble(br.readLine());
Lling = 3.14*jari*jari;
}
else{
System.out.println(“Keliling “);
System.out.print(“Inputkan diameter : “);
diameter = Double.parseDouble(br.readLine());
Kling = 3.14*diameter;
}
break;
case 3 :
if(pilH.equalsIgnoreCase(“Luas”)){
System.out.println(“Luas “);
System.out.println(“Alas : “+alas);
System.out.println(“Tinggi : “+tinggi);
System.out.println(“Hasil Luas Segitiga : “+Lsegitiga);
}
else{
System.out.println(“Keliling “);
System.out.println(“A : “+a);
System.out.println(“B : “+b);
System.out.println(“C : “+c);
System.out.println(“Hasil Keliling : “+Ksegitiga);
}
break;
case 4 :
if(pilH2.equalsIgnoreCase(“Luas”)){
System.out.println(“Luas “);
System.out.println(“Jari – jari : “+jari);
System.out.println(“Hasil Luas Lingkaran : “+Lling);
}
else{
System.out.println(“Keliling “);
System.out.println(“Diameter : “+diameter);
System.out.println(“Hasil Keliling Lingkaran : “+Kling);
}
break;
case 5 :
System.exit(0);
break;
}
}while(true);
}
catch(Exception e){
System.out.println(“Maaf anda harus menginputkan berupa angka”);
}
}
}

Published by

Admin SJN

Stikom Java Network is a UKP in Institut Bisnis dan Informatika Stikom Surabaya. We will grow up to be a biggest UKP in STIKOM for sure.

Leave a Reply

Your email address will not be published. Required fields are marked *