contoh java else if ganjil genap
Ganjil Genap
import java.util.Scanner;
public class ganjil_genap {
public static void main(String[ ] args) {
Scanner s=new Scanner(System.in);
System.out.printf("Input sebuah bilangan : ");
int bilangan=s.nextInt();
if (bilangan % 2 == 1) {
System.out.printf(bilangan+" Adalah bilangan GANJIL ");
}
else
System.out.printf(bilangan+" Adalah bilangan GENAP ");
}
}
Lampu merah else if
public class lampu{
public static void main (String[]args){
int a = 4;
if(a == 1){
System.out.print("Merah");
}
else if(a == 2){
System.out.print("Kuning");
}
else if(a == 3){
System.out.print("Hijau");
}
else{
System.out.print("Not Valid");
}
}
}
Menghitung Keliling
import java.util.Scanner;
public class bacadata{
public static void main(String[] args){
double panjang, lebar, keliling;
// Buat objek Scanner
Scanner scan = new Scanner(System.in);
System.out.println("Menghitung kelling persegi panjang");
System.out.println("Panjang: ");
Scanner Panjang = new Scanner(System.in);
panjang = scan.nextDouble();
System.out.println("Lebar ");
Scanner Lebar = new Scanner(System.in);
lebar = scan.nextDouble();
keliling = 2 * (panjang + lebar);
System.out.println("Keliling = " + keliling);
//tutup objek berkelas Scanner
scan.close();
}
import java.Util.Scanner;
public class testif{
public static void main(String[]args){
System.out.print("Masukan nama anda : ");
Scanner nm = new Scanner(System.in);
String nm = "Yuda";
if(nm == "Yuda"){
System.out.print(nm);
}
else{
System.out.print("Nama bukan Yuda");
}
}
}
}
0 komentar:
Posting Komentar