Which fragment is an example of inappropriate use of assertions? ()
A: assert (!(map.contains(x))); map.add(x);
B: if (x > 0){}else { assert (x==0); }
C: public void aMethod(int x) { assert (x > 0); }
D: assert (invariantCondition()); return retval;
E: switch (x) { case 1: break; case 2: creak; default: assert (x == 0);
A: assert (!(map.contains(x))); map.add(x);
B: if (x > 0){}else { assert (x==0); }
C: public void aMethod(int x) { assert (x > 0); }
D: assert (invariantCondition()); return retval;
E: switch (x) { case 1: break; case 2: creak; default: assert (x == 0);
举一反三
- 若有定义语句int a, b;double x;则下列选项中正确的是( ). A: switch(x%2) { case 0: a++; break; case 1: b++; break; default : a++; b++; } B: switch((int)x/2.0) { case 1: b++; break; case 0: a++; break; default : a++; b++; } C: switch((int)x%2) { case 0: a++; break; case 1: b++; break; default : a++; b++; } D: switch((int)(x)%2) { case 0.0: a++; break; case 1.0: b++; break; default : a++; b++; }
- 以下switch语句结构哪一个是正确的?( ) A: int a=1,b=0,c1,c2,x,y;switch (a*a+b*b) { case 3: case 1: y=a+b; break; case 3: y=b-a; break;} B: int a=1,b=0,c1,c2,x,y;switch a ( ) { case c1: y=a-b; break; case c2: x=a*b; break; default:x=a+b;} C: int a=1,b=0,c1,c2,x,y;switch (a*a+b*b) { default:y=a*b;break; case 3: x=a+b; break; case 10: case 11:y=a-b; break;} D: int a=1,b=0,c1,c2,x,y;switch (a+b); { case 1: y=a+b; break; case 0: y=a-b; break; }
- 中国大学MOOC: 读下列程序:void main(){ int x=1,y=0,a=0,b=0; switch(x) { case 1: switch(y) { case 0: a++; break; case 1: b++; break; } case 2: a++; b++; break; } printf(
- 有如下程序: void main() { int x=1,a=0,b=0; switch (x) { case 0: b++; break; case 1: a++; break; case 2: a++;b++; break; } printf("a=%d,b=%d",a,b); }
- 有如下程序段: int x =1, y=1 ; int m , n; m=n=1; switch (m) { case 0 : x=x*2; case 1: { switch (n) { case 1 : x=x*2; case 2 : y=y*2;break; case 3 : x++; } } case 2 : x++;y++; case 3 : x*=2;y*=2;break; default:x++;y++; } 执行完成后,x和y 的值分别为( )。