Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

If

public class HelloWorld
{
     public static void main(String []args)
     {
        int x = 0, y = 0, z = 0, k = 0;
         
        if ( x == y ) z = 9;     // x and y are equal so the boolean result is true and the statement is executed.
        System.out.println("z=" + z);

        z = 0;
        
        if ( x == y )
            z = 9;               // This is the same as above, the single statement can be on the next line.
        System.out.println("z=" + z);

        z = 0;
        
        if ( x == y )            // The two statements in the block are executed.
        {
            z = 9;
            z = x + 3;
        }
    
        System.out.println("z=" + z);

        if ( x < -1) y = 2;     // x is not greater than -1 (it is zero) so the statement is not executed.
        System.out.println("y=" + y);

        x = y = z = 0;
        
        if ( x == y )
            z = 7;                // This statement is executed.
        else
            z = 0;
        
        System.out.println("z=" + z);

        if ( x != y )
            z = 7;
        else
            z = 0;                // This statement is executed.
        
        System.out.println("z=" + z);

        if ( x == 0 )
        {
            z = 99;               // This statement block is executed
            k = 33;
        }
        else
        {
            z++;
            k += 2;
        }
        
        System.out.println("z=" + z + "  k=" + k);

        x = y = z = 0;
        
        if ( x == 2 )
            z = 5;
        else if ( y == 0 ) 
            z = 2;              // This statement is executed.
        
        System.out.println("z=" + z);

        k= z = 0;
        
        if ( x == 2 )
            z = 5;
        else if ( y == 1 ) 
            z = 2;              // Neither statement is executed, both boolean results are false.
        
        System.out.println("z=" + z);

        // You can also do:

        z = 0;
        
        if ( x == 9 )
            z = 5;
        else if ( y == 9 ) 
            z += 2;
        else                    // Neither of the above comparisons are true so this else is excuted.
            k = z = 9;  
           
        System.out.println("z=" + z);
     }
}

While

public class HelloWorld
{

     public static void main(String []args)
     {
        System.out.println("WHILE statement examples.");
        
        int k = 0, x = 0, y = 0, z = 0;
        
        while ( z < 10 ) z += 1;             // z is incremented repeatedly as long as z is less than 10.

        System.out.println("1 z=" + z + " it should be 10.");      
        
        z = 0;
        
        while ( z < 10 )
        {
            z += 1;                          // z and k are incremented repeatedly as long as z is less than 10.
            k += 2;
        }

        System.out.println("2 k=" + k + " it should be 20.");
 
        while ( x < 5 )
        {
            if ( x == 3 ) break;            // while should stop when x is 3.
            x += 1;
        }

        System.out.println("3 x=" + x + " it should be 3.");
        
        x = y = 0;
        
        while ( y < 10 )
        {
            y++;
            if ( x == 3) continue;          // while should run 10 times but x should
            x += 1;                         // only be incremented 3 times.
        }

        System.out.println("4 x=" + x + " it should be 3. y=" + y + " it should be 10.");
     }
}

Compile and Execute Java8 Online

public class Belajar06{

     public static void main(String []args){
        System.out.println("Belajar06");
        int A, B;
        A = 2;
        B = 3;
        System.out.println("A&B = " + (A&B));
        System.out.println("A|B = " + (A|B));
        System.out.println("A^B = " + (A^B));
        
     }
}

Belajar1.java

public class HelloWorld{

     public static void main(String []args){
        System.out.println("Hello World");
     }
}

Compile and Execute Java8 Online

public class Belajar03{

     public static void main(String []args){
        System.out.println("Belajar03");
        int panjang, lebar, luas, keliling;
        panjang = 10;
        lebar = 15;
        luas = panjang * lebar;
        System.out.println("Luas persegi panjang = " + luas);
        keliling = 2 * (panjang * lebar);
        System.out.println("keliling persegi panjang = " + keliling);
        
     }
}

DiceRoller

import java.util.Random;
/**
 * Rolls a pair of six sided dice
 * 
 * @author Jackson Sottile
 */
public class DiceRoller {
    
    public static void main(String[]args) {
        Random rand = new Random();//pseudo random numbers
        
        // roll the dice and compute the sum
        int die1 = 1 + rand.nextInt(6);
        //generates random int between 0(inclusive) and 6(exclusive)
        int die2 = 1 + rand.nextInt(6);
        int sum = die1 + die2;
        
        System.out.println("die 1: " + die1);
        System.out.println("die 2: " + die2);
        System.out.println("Sum = " + sum);
        
    }
     
}

Compile and Execute Java8 Online

public class HelloWorld{

     public static void main(String []args){
        System.out.println("Hello World");
     }
}

Swap max and min in array

import java.io.*;
import java.util.*;

public class Hello{
    public static int getMinIndex(int[] array){
    int minIndex = 0;
    for (int i = 1; i <= array.length - 1; i++){
        if (array[i] < array[minIndex]){
        minIndex = i;
        }
    }
    return minIndex;
    }
    
    public static int getMaxIndex(int[] array){
        int maxIndex = 0;
        for (int i = 1; i<array.length; i++){
            if (array[i] > array[maxIndex]){
                maxIndex = i;
            }
        }
        return maxIndex;
    }
    
    public static int[] swap (int[] array){
        int minIndex = getMinIndex(array);
        int maxIndex = getMaxIndex(array);
        int temp = array[minIndex];
        array[minIndex] = array[maxIndex];
        array[maxIndex] = temp;
        return array;
    }
    
    public static void main (String args[]){
        int[] array = {10,2,30};
        System.out.println(Arrays.toString(swap(array)));
    }
}

ConstructorDemo

/**
 * Demonstrates how to create objects and initialze them
 * with constructors.
 * 
 * @author Jackson Sottile
 */
public class ConstructorDemo {

     public static void main(String []args) {
         Random rand = new Random();//create objects with keyword new.
         //Random doesn't take an argument.
         
     }
}

Dice

/**
 * Demonstrates how to create objects and initialize them
 * with constructors.
 * 
 * @author Xavier Char-Amato
 */
import java.util.Random;

public class Dice{
    public static void main(String[] args){
        Random rand = new Random();
        
        int[] dice = new int[2];
        dice[0] = 1 + rand.nextInt(6);
        dice[1] = 1 + rand.nextInt(6);
        
        int sum = 0;
        
        for (int i=0; i<dice.length; i++){
            System.out.println("Die #" + (i+1) +" "+ dice[i]);
            sum += dice[i];
        }
        System.out.println("Sum "+sum);
        
        
    
    }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.