2009年3月2日 星期一

Lab Java operators

(a) Write a Java program as follows:

Let i=2;
Print i;
Print 2 * (i++);
Print i;

Ans: 2, 4, 3

(b) Write a Java program as follows:

Let i=2;
Print i;
Print 2 * (++i);
Print i;

Ans: 2, 6, 3
------------------------------------------------<程式碼開始>--------------------------------------

public class hanyi
{
public static void main( String[] args )
{
int i=2;
System.out.println( "(a) Write a Java program as follows:\ni = 2");
System.out.println( "Print i = "+ i );
System.out.println( "Print 2 * (i++) = " +  2*(i++));
System.out.println( "Print i = " + i );
i=2;
System.out.println( "\n(b) Write a Java program as follows:\ni = 2");
System.out.println( "Print i = "+ i );
System.out.println( "Print 2 * (++i) = " +  2*(++i));
System.out.println( "Print i = " + i );
}
}

------------------------------------------------<程式碼結束>--------------------------------------

沒有留言:

張貼留言