Praktikum Pemrograman
--:: IF dan Switch ::--
--java16--
package javadasar;
public class java16 {
public static void main(String[]args){
int x = 3; Nilai variabel bisa dirubah
if (x > 0){
System.out.println(+x+" adalah Bilangan
Positif");
} else {
System.out.println(+x+" adalah Bilangan
Negatif");
}
}
}
--java17--
package javadasar;
import java.util.Scanner;
public class java17 {
public static void main(String[]args){
Scanner coba = new Scanner(System.in);
int a, b;
System.out.println("Kondisi IF-ELSE");
System.out.println("----------------------");
System.out.print("Nilai A = ");
a = coba.nextInt();
System.out.print("Nilai B = ");
b = coba.nextInt();
System.out.println("----------------------");
if (a == b) {
System.out.println("Nilai A = " +a+ "
SAMA dengan Nilai B = " +b);
} else {
System.out.println("Nilai A = " +a+ "
TIDAK SAMA dengan Nilai B = " +b);
}
}
}
Netbeans IDE - Java 13
Praktikum Pemrograman
--java18--
package javadasar;
public class java18 {
public static void main(String[]args){
int jam = 18;
if (jam < 12){
System.out.println("SELAMAT PAGI");
} else if (jam <16){
System.out.println("SELAMAT SIANG");
} else if (jam <19){
System.out.println("SELAMAT SORE");
} else if (jam <24){
System.out.println("SELAMAT MALAM");
} else {
System.out.println("SALAH JAM");
}
}
}
--java19--
package javadasar;
public class java19 {
public static void main(String[]args){
char huruf = 'X';
switch (huruf) {
case 'y':
System.out.println("Hurufnya y"); break;
case 'X':
System.out.println("Hurufnya X"); break;
case 'Z':
System.out.println("Hurufnya Z"); break;
default :
System.out.println("Salah Huruf");
break;
}
}
}
Netbeans IDE - Java 14
Praktikum Pemrograman
--java20--
package javadasar;
public class java20 {
public static void main(String[]args){
int hari = 1;
switch (hari) {
case 1:
System.out.println("Hari Minggu");
break;
case 2:
System.out.println("Hari Senin");
break;
case 3:
System.out.println("Hari Selasa");
break;
case 4:
System.out.println("Hari Rabu");
break;
case 5:
System.out.println("Hari Kamis");
break;
case 6:
System.out.println("Hari Jumat");
break;
case 7:
System.out.println("Hari Sabtu");
break;
default:
System.out.println("Salah Hari");
}
}
}
--java21--
package javadasar;
import java.util.Scanner;
public class java21 {
public static void main(String[]args){
Scanner in = new Scanner (System.in);
String nama , nim ;
int jurusan;
System.out.print("Masukan Nama Peserta : ");
nama = in.nextLine();
System.out.print("Masukan Nomer Peserta : ");
nim = in.nextLine();
System.out.print("Pilihan Jurusan : \n 1. Teknik Informatika \n 2. Teknik
Industri \n 3. Teknik Sipil \n 4. Teknik Mesin \n\n Masukan pilihan jurusan :
");
jurusan = in.nextInt();
switch(jurusan) {
Netbeans IDE - Java 15
Praktikum Pemrograman
case 1:
System.out.println("\n"+nama+" - "+nim+" memilih jurusan Teknik
Informatika");
break;
case 2:
System.out.println("\n"+nama+" - "+nim+" memilih jurusan Teknik
Industri");
break;
case 3:
System.out.println("\n"+nama+" - "+nim+" memilih jurusan Teknik
Sipil");
break;
case 4:
System.out.println("\n"+nama+" - "+nim+" memilih jurusan Teknik
Mesin");
break;
default:
System.out.println("Tidak ada jurusannya");
break;
}
}
}
Netbeans IDE - Java 16