Selasa, 10 Januari 2012

Perulangan pada Java

Kelas perulangan

    A.//menampilkan tabel Perkalian
        public class Perulangan {
        public static void main (String[] args) {
        int a,b,c;
        System.out.println("----------------------------");
        System.out.println("i\t i*2\t i*3\t i*4");
        System.out.println("----------------------------");
        for (int i=1;i<5;i++) {
        System.out.print(i + "\t");
        a=i*2;
        b=i*3;
        c=i*4;
        System.out.print(a + "\t");
        System.out.print(b + "\t");
        System.out.print(c + "\n");
        }}}
   
                     B.//menampilkan belah ketupat (*)
        public class Perulangan2 {
        public static void main(String args[]) {

        int n=5;
        int a, b, c;
        a = 1; c=n-1; 
        
        for (int i=0; i < n*2-1; i++)  { 
        if (i < n) {  
        for (int j=0; j < n*2-1; j++) { 
        b=n-a; 
        if ((j < b ) || (j >= (b+2*a-1))) { 
        System.out.print(" "); 
        }  
        else { 
        System.out.print("*"); 
        }   
        } 
        a = a+1; 
        }   
        else {  
        for (int j=0; j < n*2-1; j++) { 
        b=n-c;
        if ((j < b) || (j >= (b+2*c-1)) ) { 
        System.out.print(" "); 
        }  
        else { 
        System.out.print("*");   
        }   
        }
        c = c-1;  
        }  
        System.out.println(); 
        }}} 

   C. //Menampilkan setengah belah ketupat(*)
        public class TengahBin {
     public static void main (String[] args) {
    
     for (int y=1; y<=5; y++) {
     for (int x=1; x<=y; x++) {
           System.out.print ("*");
     }
     System.out.println ();
     }
     for (int y=1; y<=5; y++) {
     for (int x=5; x>=y; x--) {
           System.out.print ("*");
     }
     System.out.println ();
     }}}


    D. //Menampilkan 100 nama menggunakan "while loop,Do-while loop dan for loop.
    public class Wl {
   public static void main( String[] args ){
   //while loop
/*int i = 4;
while ( i > 0 ){
System.out.print(i);
i--;
}
int x = 0;
while (x<10)
{
System.out.println(x);
x++;
}
//infinite loop
int i=0;
while(i<100){
System.out.println("noni");
i++;
}

//Do-while loop
int x = 0;
do
{
System.out.println(x);
x++;
}while (x<10);

//infinite loop
int i=0;
do{
System.out.println("noni");
i++;
} while (i<100);

//for loop
int i;
for( i = 0; i < 10; i++ ){
System.out.print(i);
}
*/
int i;
for(i=0;i<100;i++){
System.out.println("noni");
}}}

    E.//Menampilkan Kalkulator Aritmatika menggunakan switch

               public class Kalkulator{
 public static void main (String[] args){
 String ulangi = "Y";
     do {
     System.out.println("KALKULATOR ARITMATIKA");
     System.out.println("1. Tambah (+)");
     System.out.println("2. Kurang (-)");
     System.out.println("3. Kali\t  (x)");
     System.out.println("4. Bagi\t  (:)");
     int operator;
     float angka1, angka2;
     float hasil = 1;
    
     System.out.print("\nPilih Operator Aritmatika (1/2/3/4) : ");
     operator  =  Integer.parseInt(System.console(). readLine());
     System.out.print("\nMasukkan angka pertama : ");
     angka1  =  Float.parseFloat(System.console(). readLine());
     System.out.print("\nMasukkan angka kedua : ");
     angka2  =  Float.parseFloat(System.console(). readLine());
      
     switch(operator) {
           case 1 : hasil = angka1 + angka2;
           break;
           case 2 : hasil = angka1 - angka2;
           break;
           case 3 : hasil = angka1 * angka2;
           break;
           case 4 : hasil = angka1 / angka2;
           break;
           default : System.out.println("tidak ada pilihan operator");
           }
     System.out.print("Hasil : " + hasil);
      
     System.out.print("\nulangi (Y/N) ? ");
     ulangi = System.console().readLine();
          
     } while (ulangi.equals("Y") || ulangi.equals("y"));
}}

   F. //Menampilkan output dari case yang terpilih
   public class Grade{
public static void main( String[] args ){
System.out.println("Masukkan Nilai ??? ");
     String grade = System.console().readLine();
    int grade1 = Integer.parseInt(grade);
/*if( grade1 >= 90 ){
System.out.println( "Excellent!" );
}
else if( (grade1 < 90) && (grade1 >= 80)){
System.out.println("Good job!" );
}
else if( (grade1 < 80) && (grade1 >= 60)){
System.out.println("Study harder!" );
}
else{
}*/
switch(grade1){
case 100:
System.out.println( "Excellent!" );
break;
case 90:
System.out.println("Good job!" );
break;
case 80:
System.out.println("Study harder!" );
break;
default:
System.out.println("Sorry, you failed.");
}}}

          G. //Menampilkan gambar Zigzag menggunakan perulangan
   public class Zigzag {
public static void main (String[] args){
int l, t, i, j, f;
int c = 219;
 
System.out.println("Menggambar Zigzag");
    
System.out.print("l :" );
l  =  Integer.parseInt(System.console(). readLine());
 System.out.print("t :" );
t  =  Integer.parseInt(System.console(). readLine());
    
System.out.println ();
for(i = 1; i <= t; i++) {
System.out.print("\t");

if(i > 1 && i < t) {
for(j = 1; j <= l; j++) {
if(j % 2 == 1) {
System.out.print((char)c);
} else {
System.out.print(" ");
}
}
} else {
if(i == 1) {
  f = 2;
} else {
f = 4;
}
for(j = 1; j <= l; j++) {
if(j == f){
System.out.print(" ");
f+=4;
} else {
System.out.print((char)c);
}
}
System.out.println ();
}}}

0 komentar:

Posting Komentar

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hosted Desktop