Program Struktur Kontrol

 if

import java.io.*;

class BR{

public static void main (String[]args){

String input;

int num;

DataInputStream in = new DataInputStream(System.in);

System.out.println(“Masukan bilangan anda : “);

try{

input = in.readLine();

num = Integer.parseInt(input);

if (num > 0)

System.out.println(num+ “\tBilangan Positif”);

if (num < 0)

System.out.println(num+ “\tBilangan Negatif”);

}catch (Exception e){

}

}

}

if-else

import java.io.*;

public class if_else{

public static void main (String[]args){

String input;

int num;

DataInputStream apa = new DataInputStream(System.in);

try{

System.out.println(“Masukan Bilangan anda :”);

input = apa.readLine();

num = Integer.parseInt(input);

if (num%2 == 0)

System.out.println(“Aku Bilangan genap”);

else

System.out.println(“Aku Bilangan Ganjil”);

}

catch (Exception e)

{}

}

}

 if-else-if

import java.io.*;

public class if_else_if{

public static void main (String[]args){

String a,c;

float num1,num2;

char b;

DataInputStream apa = new DataInputStream(System.in);

try

{

System.out.println(“Masukan Bilangan ke 1 :”);

a = apa.readLine();

num1 = Float.parseFloat(a);

System.out.println(“Masukan Oprasi :”);

c = apa.readLine();

b = c.charAt(0);

System.out.println(“Masukan Bilangan ke 2 :”);

a = apa.readLine();

num2 = Float.parseFloat(a);

if (b == ‘+’){

int hsl =(int) (num1 + num2);

System.out.println(+num1+ “+” +num2+ “=” + hsl);}

else if (b == ‘-‘){

int hsl = (int)(num1 + num2);

System.out.println(+num1+ “-” +num2+ “=”);}

else if (b == ‘*’){

int hsl = (int)(num1 + num2);

System.out.println(+num1+ “*” +num2+ “=”);}

} catch (Exception e){}

}

}

if-else-else-if

public class if_else_else_if{

public static void main (String[]args){

int nilai = 75;

char indeks;

System.out.println(“Nilai anda adalah”);

if (nilai >= 90){

System.out.println(“A”);

}

else if (nilai >= 80){

System.out.println(“B”);

}

else if (nilai >= 70){

System.out.println(“C”);

}

}

}

Switch-case

class switch_case{

public static void main (String[]args){

int hari = 4;

switch (hari){

case 0: System.out.println(“Minggu”);

break;

case 1: System.out.println(“Senin”);

break;

case 2: System.out.println(“Selasa”);

break;

case 3: System.out.println(“Rabu”);

break;

case 4: System.out.println(“Kamis”);

break;

case 5: System.out.println(“Jumat”);

break;

case 6: System.out.println(“Sabtu”);

break;

default:

System.out.println(“Diluar Range hari”);

}

}

}

Iklan

One Response to Program Struktur Kontrol

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: