作业2:习题
1、 判断以下程序的输出结果:
1 public class Test1 { 2 public static void main(String[] args) { 3 int x = 10; 4 int a = x + x++; 5 System.out.println("a="+a); 6 System.out.println("x="+x); 7 int b = x + ++x; 8 System.out.println("b="+b); 9 System.out.println("x="+x); 10 int c = x + x--; 11 System.out.println("c="+c); 12 System.out.println("x="+x); 13 int d = x + --x; 14 System.out.println("d="+d); 15 System.out.println("x="+x); 16 } 17 }
运行结果:
2、
1 public class Test2 { 2 public static void main(String[] args) { 3 int a = 15; 4 int b = 2; 5 double c = 2; 6 System.out.println(a+"/"+b+"="+(a/b)); 7 System.out.println(a+"%"+b+"="+(a%b)); 8 System.out.println(a+"/"+c+"="+(a/c)); 9 System.out.println(a+"%"+c+"="+(a%c)); 10 } 11 }
运行结果:
3、
1 public class Test3 { 2 public static void main(String[] args) { 3 boolean x,y,z; 4 int a = 15; 5 int b = 2; 6 x = a>b; 7 y = a<b; 8 z = a!=b; 9 System.out.println("x="+x); 10 System.out.println("y="+y); 11 System.out.println("z="+z); 12 } 13 }
运行结果:
4、 判断以下程序的输出结果:
1 public class Test4 { 2 public static void main(String[] args) { 3 int x; 4 double y; 5 x = (int)22.5+(int)34.7; 6 y = (double)x; 7 System.out.println("x="+x); 8 System.out.println("y="+y); 9 } 10 }
输出结果:
5、判断以下程序的输出结果:
1 public class Test5 { 2 public static void main(String[] args) { 3 int i = 5; 4 int j = 5; 5 int m = 5; 6 int n = 5; 7 i++; //这行代码执行结束之后,i是6 8 j = j+1; //这行代码执行结束之后,j是6 9 m--; //这行代码执行结束之后,m是4 10 n = n-1; //这行代码执行结束之后,n是4 11 System.out.println(i); 12 System.out.println(i++);6(虽然输出结果是6,但是这行代码执行结束之后,i是7) 13 System.out.println(++i); 14 System.out.println(i--); 15 System.out.println(--i); 16 System.out.println(); 17 System.out.println(j); 18 System.out.println(j++); 19 System.out.println(j--); 20 System.out.println(--j); 21 System.out.println(); 22 System.out.println(m); 23 System.out.println(n); 24 } 25 }
运行结果:
6、判断以下程序的输出结果
1 public class Test6 { 2 public static void main(String[] args) { 3 int i = 0; 4 int j = 0; 5 System.out.println(i); 6 System.out.println(j); 7 i++; 8 ++j; 9 System.out.println(i); 10 System.out.println(j); 11 System.out.println("---------------"); 12 System.out.println(i++); 13 System.out.println(++j); 14 System.out.println("---------------"); 15 System.out.println(i); 16 System.out.println(j); 17 } 18 }
运行结果: