Contoh Program If..Else dan Looping pada bahasa pemrograman Java
1. IF...Else
package ifelse;
import java.io.*;
class Main {
public static void main(String[] args) {
InputStreamReader istream = new InputStreamReader(System.in);
BufferedReader bufRead = new BufferedReader (istream);
System.out.println("Welcome/Selamat datang");
try{
System.out.print("masukkan nilai = ");
String tes1 = bufRead.readLine();
double tes = Integer.parseInt(tes1);
if(tes == 1){
System.out.println("angka yang anda masukkan bernilai 1");
}else{
System.out.println("angka yang anda masukkan tidak bernilai 1");
}
}
catch(IOException err){
}
}
}
2. Looping
package javaapplication3;
import java.io.*;
class Main {
public static void main(String[] args) {
InputStreamReader istream = new InputStreamReader(System.in);
BufferedReader bufread = new BufferedReader (istream);
System.out.println("selamat datang/welcome");
try{
System.out.println("masukkan jumlah bintang");
String x = bufread.readLine();
int z = Integer.parseInt(x);
for(int i=1;i<=z;i=i+1){
for(int j=1;j<=i;j=j+1){
System.out.print("*");
}
System.out.println();
}
for(int a=z-1;a<=z;a=a-1){
for(int b=1;b<=a;b=b+1){
System.out.print("*");
}
System.out.println();
}
}
catch (IOException err){
System.out.print("Error");
}
catch(NumberFormatException err) {
System.out.println("Error Converting Number");
}
}
}
terima kasih
semoga bermanfaat
Tidak ada komentar:
Posting Komentar