Java Lecture3 OVR

Embed Size (px)

Citation preview

  • 7/28/2019 Java Lecture3 OVR

    1/11

    1

    Java 2

    . .

    if, ?:, switch

    for, while, do while

    /

    break, continue

    return, System.exit(status)

  • 7/28/2019 Java Lecture3 OVR

    2/11

    2

    . .

    { }

    { int x = 2; }

    System.out.println(x);

    : Can not resolve symbol x !

    . .

    if

    if()

    ;else

    ;

    if(){ }

    else{ }

  • 7/28/2019 Java Lecture3 OVR

    3/11

    3

    . .

    if

    if(x5) && (y5){

    System.out.println(x is larger than 5);System.out.println(You win !!!);

    }else

    System.out.println(You lost I am sorry.);

    . .

    ?:

    ()?:

    System.out.println( (x

  • 7/28/2019 Java Lecture3 OVR

    4/11

    4

    . .

    switch

    switch(x)

    {case (1 x): ;case (2 x): {}default: ;

    } if

    else if

    . .

    for

    for(x = ; ; x);

    for(x = ; ; x){

    ;}

    for(x1 = 1, x2 = 2, ...; ; x1, x2, ... )

    for(;;) { }

  • 7/28/2019 Java Lecture3 OVR

    5/11

    5

    . .

    for

    for(i = 1; i

  • 7/28/2019 Java Lecture3 OVR

    6/11

    6

    . .

    while

    int x = 0;while (x

  • 7/28/2019 Java Lecture3 OVR

    7/11

    7

    . .

    do while

    int x = 0;do{System.out.println(x++);

    } while (x

  • 7/28/2019 Java Lecture3 OVR

    8/11

    8

    . .

    break

    class LabelTest {

    public static void main (String arguments[]) {

    for (int j = 1; j

  • 7/28/2019 Java Lecture3 OVR

    9/11

    9

    . .

    continue

    { continue; }

    Label: { continue Label; }

    ,

    . .

    continue

    class LabelTest {

    public static void main (String arguments[]) {

    for (int j = 1; j

  • 7/28/2019 Java Lecture3 OVR

    10/11

    10

    . .

    :

    return; main()

    System.exit(0)

    . .

    class CopyArrayWhile {

    public static void main (String arguments[]) {

    int[] array1 = { 7, 4, 8, 1, 4, 1, 4 };

    float[] array2 = new float[array1.length];

    System.out.print("array1: [ ");

    for (int i = 0; i < array1.length; i++) {

    System.out.print(array1[i] + " ");

    }System.out.println("]");

    System.out.print("array2: [ ");

    int count = 0;

    while ( count < array1.length && array1[count] != 1){

    array2[count] = (float) array1[count];

    System.out.print(array2[count++] + " ");

    }

    System.out.println("]");

    }

    }

  • 7/28/2019 Java Lecture3 OVR

    11/11

    11

    . .

    (Basic.java)

    . .

    if, switch.

    for, while, do...while

    break

    H continue

    H return main() System.exit(0)