Search Blog

Biyernes, Enero 18, 2013

Write a program that demonstrates the difference between predecrementing and postdecrementing using the decrement operator --.

Java Codes
--+o0O0o+--

Write a program that demonstrates the difference between predecrementing and postdecrementing using the decrement operator --. 


Code:



import java.util.Scanner;

public class MachineProblem12 {

    public static void main(String[] args) {
       
        Scanner input_user = new Scanner(System.in);
       
        int base_num,loop_num,base_num1,x,y;
       
        System.out.println("*****************************************");
       
            System.out.print("  Enter base number: ");
            base_num = input_user.nextInt();
           
            base_num1 = base_num;
           
            System.out.print("  Enter number of loop: ");
            loop_num = input_user.nextInt();
           
            x = 0;
            y = 0;
           
            for(int z = 0;z < loop_num;z++)
            {
                if(z == 0)
                {  
                    x = --base_num;
                    y = base_num1--;
                    System.out.println("");
System.out.println(" xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
System.out.println(" ||# of LOOP||Predecrement||Postdecrement  ||");
System.out.println(" xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
System.out.println(" ||   1st   ||     " + x + "         " + y );
                    System.out.println(" xxxxxxxxxxxxxxx");
                }
                if(z == 1)
                {  
                    x = --base_num;
                    y = base_num1--; 
System.out.println(" ||   2nd   ||     " + x + "         " + y);
                    System.out.println(" xxxxxxxxxxxxxxx");
                }
              


 if(z == 2)
                {  
                    x = --base_num;
                    y = base_num1--;
System.out.println(" ||    3rd    ||      " + x + "      " + y);
                    System.out.println(" xxxxxxxxxxxxxxx");
                }
                if(z > 2)
                {  
                    x = --base_num;
                    y = base_num1--;
System.out.println(" || " + (z + 1) +"th|| " + x + "    " + y);
                    System.out.println(" xxxxxxxxxxxxxxx");
                }
            }
    }
}

Walang komento:

Mag-post ng isang Komento