penggunaan statement "switch"
di sini adalah contoh penggunaan statement switch dan looping do-while..............
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author matt
*/
import java.io.*;
public class newClass
{
public static void main(String[]args)throws Exception
{
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
do{
System.out.println("=============================");
System.out.println(" simple math function ");
System.out.println("=============================");
System.out.println(" MENU ");
System.out.println("=============================");
System.out.println("1.panjang dan lebar");
System.out.println("2.sisi");
System.out.println("3.luas persegi panjang");
System.out.println("4.luas bujur sangkar");
System.out.println("5.EXIT");
System.out.println("=============================");
System.out.println("=============================");
System.out.println("");
System.out.println("");
System.out.print("masukkan pilihan anda :");
DataInputStream dis = new DataInputStream(System.in);
int calc = Integer.parseInt(dis.readLine());
switch (calc)
{
case 1:
System.out.println("=============================");
System.out.println("panjang dan lebar");
System.out.println("=============================");
System.out.print("masukkan panjang :");
a =Integer.parseInt(dis.readLine());
System.out.print("masukkan lebar :");
b =Integer.parseInt(dis.readLine());
System.out.println("");
System.out.println("");
break;
case 2:
System.out.println("=============================");
System.out.println("sisi");
System.out.println("=============================");
System.out.println("masukkan sisi :");
c=Integer.parseInt(dis.readLine());
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
break;
case 3:
System.out.println("=============================");
System.out.println("luas persegi panjang");
System.out.println("=============================");
d = a*b;
System.out.println("luasnya adalah :"+d);
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
break;
case 4:
System.out.println("=============================");
System.out.println("4.luas bujur sangkar");
System.out.println("=============================");
e =4*c;
System.out.println("luasnya adalah :"+e);
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
break;
case 5:
System.exit(0);
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
break;
}
}while(true);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author matt
*/
import java.io.*;
public class newClass
{
public static void main(String[]args)throws Exception
{
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
do{
System.out.println("=============================");
System.out.println(" simple math function ");
System.out.println("=============================");
System.out.println(" MENU ");
System.out.println("=============================");
System.out.println("1.panjang dan lebar");
System.out.println("2.sisi");
System.out.println("3.luas persegi panjang");
System.out.println("4.luas bujur sangkar");
System.out.println("5.EXIT");
System.out.println("=============================");
System.out.println("=============================");
System.out.println("");
System.out.println("");
System.out.print("masukkan pilihan anda :");
DataInputStream dis = new DataInputStream(System.in);
int calc = Integer.parseInt(dis.readLine());
switch (calc)
{
case 1:
System.out.println("=============================");
System.out.println("panjang dan lebar");
System.out.println("=============================");
System.out.print("masukkan panjang :");
a =Integer.parseInt(dis.readLine());
System.out.print("masukkan lebar :");
b =Integer.parseInt(dis.readLine());
System.out.println("");
System.out.println("");
break;
case 2:
System.out.println("=============================");
System.out.println("sisi");
System.out.println("=============================");
System.out.println("masukkan sisi :");
c=Integer.parseInt(dis.readLine());
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
break;
case 3:
System.out.println("=============================");
System.out.println("luas persegi panjang");
System.out.println("=============================");
d = a*b;
System.out.println("luasnya adalah :"+d);
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
break;
case 4:
System.out.println("=============================");
System.out.println("4.luas bujur sangkar");
System.out.println("=============================");
e =4*c;
System.out.println("luasnya adalah :"+e);
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
break;
case 5:
System.exit(0);
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
break;
}
}while(true);
}
}
Komentar
Posting Komentar