单选题研究下面的Java代码:  switch (x) {  case 1:  System.out.println(1);  case 2:  case 3:  System.out.println(3);  case 4:  System.out.println(4); }  当x=2时,运行结果是()。A 没有输出任何结果B 输出结果为3C 输出结果是3和4D 输出结果是1、3和4

题目
单选题
研究下面的Java代码:  switch (x) {  case 1:  System.out.println(1);  case 2:  case 3:  System.out.println(3);  case 4:  System.out.println(4); }  当x=2时,运行结果是()。
A

没有输出任何结果

B

输出结果为3

C

输出结果是3和4

D

输出结果是1、3和4


相似考题
更多“研究下面的Java代码:  switch (x) {  case 1:  System.out.println(1); ”相关问题
  • 第1题:

    有以下程序:includemain(){intx=1,y=0,a=0,b=0;switch(x){case 1:switch(y){case 0:a+

    有以下程序: #include<stdio.h> 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; case 3:a++;b++; } printf("a=%d,b=%d\n",a,B); } 程序的运行结果是( )。

    A.a=1,b=0

    B.a=2,b=2

    C.a=1,b=1

    D.a=2,b=1


    正确答案:D

  • 第2题:

    已知如下代码: switch(m) { case 0: System.out.println("Condition0"); case 1: System.out.println("Condition 1"); case 2: System.out.println("Condition 2"); case 3: System.out.println("Condition 3"); break; default: System.out.println("Other cCndition"); } 当m为( )时,"Condition 2"会出现在输出结果中。

    A.2

    B.0、1

    C.0、1、2

    D.0、1、2、3


    正确答案:C
    解析:在switch-case语句中,如果case后没有break,程序会继续执行后面的case语句,直到遇到break或者switch结束。这个程序中只有case 3后才有break,所以当m=0、1、2都将执行case 2后的语句。m=3时,前三个case不满足,所以不执行case 2后的语句,因此选C。

  • 第3题:

    以下代码的输出结果是什么? class Foo{ public static void main(String args[]){ int x=4,j=0; switch(x){ case 1:j++; case 2:j++; case 3:j++; case 4:j++; case 5:j++; break; default:j++; } System.out.println(j); } }()

    A.1

    B.2

    C.3

    D.编译错误


    正确答案:B

  • 第4题:

    若有以下定义:float x;int a,b; 则错误的switch语句是________。

    A. switch(x){case 1.0:printf("*\n"); case 2.0:printf("*\n"); }

    B. switch(a){case 1:printf("*\n");case 2:printf("*\n"); }

    C. switch(a+b) {case 1:printf("*\n"); case 1+2:printf("*\n"); }

    D. switch(a+b);{case 1:printf("*\n");case 2:printf("*\n"); }


    参考答案:D

  • 第5题:

    编译和执行以下代码,输出结果是( )。 int i=1; switch (i) { case 0: System.out.print("zero,"); break; case 1: System.out.print("one,"); case 2: System.out.print("two,"); default: System.out.println("default"); }

    A.one,

    B.one,two,

    C.one,two,default

    D.default


    正确答案:C

  • 第6题:

    给出下列代码段: public class ex38 { public static void main (String args [ ] ) { int m; switch(m) { case 0: System.out.println ( "case 0" ); case 1:System.out.println("case 1");break; case 2: default: System.out.print in ("default") } } 下列m的______值将引起"default"的输出。

    A.0

    B.1

    C.2

    D.以上答案都不正确


    正确答案:C

  • 第7题:

    有下列程序:includemain(){int x=1,y=0,a=0,b=0;switch(x){ case 1:switch(y){ case 0

    有下列程序: #include <stdio.h> 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; case 3: a++;b++; } printf("a=%d,b=%d\n",a,B) ; } 程序的运行结果是( )。

    A.a=1,b=0

    B.a=2,b=2

    C.a=1,b=1

    D.a=2,b=1


    正确答案:D
    解析:本题考查的是用switch语句实现多分支选择结构,首先进入第一个switch(x)判断,进入case 1中进行嵌套判断,如果switch(y)也成立,a自加1,遇到break,退出嵌套,接着执行case 2,a自加为2,b自加为1,遇到break,结束循环,此时a=2,b=1,故选项D)正确.

  • 第8题:

    若有以下定义,则正确的switch语句是______。float x;int a,b;

    A.switch(x) { case 1.0:printf("*\n"); csse 2.0:printf("**\n"); }

    B.switch(x) { case 1,2:printf("*\n"); case 3:printf("**\n"); }

    C.switch(a+b) { case 1:printf("\n"); case 1+2:printf("**\n"); }

    D.switch(a+b); { case 1:printf("*\n"); case 2:printf("**\n"); }


    正确答案:C

  • 第9题:

    给出下列的程序片段,m为( )时将会输出default。 switch (m) case ():Systemt.out.println("case 0"); case 1: System.out.println("case 1");break; case 2:System.out.println("case2");break; default:System.out.println("default");

    A.0

    B.1

    C.2 2

    D.3


    正确答案:D

  • 第10题:

    若有以下定义:float x;int a,b,c=2;,则正确的switch语句是( )

    A.switch(x) { case 1.0:printf("*\n"); case 2.0:printf("**\n"); }

    B.switch(int(x)) { case 1:printf("*\n"); case 2:printf("**\n"); }

    C.switch(a+b) { case 1:printf("*\n"); case 1+2:printf("**\n"); }

    D.switch(a+B){ case 1:printf("*\n"); case c:printf("**\n"); }


    正确答案:C

  • 第11题:

    有如下代码段:

    switch ( x ){

    case 1:System.out.println("One");break;

    case 2:

    case 3:System.out.println("Two");break;

    default:System.out.println("end");

    }

    变量x的取值下列哪些情形时,能使程序输出"Two" 。

    A. 1

    B. 2

    C. 3

    D. default


    正确答案:BC

  • 第12题:

    class TestApp{   public static void main(String[] args){   int x = 5;   switch(x){   case 1:   case 2:   case 3:   System.out.println(“一季度”);   break;   case 4:   case 5:   case 6:   System.out.println(“二季度”);   case 7:  case 8:   case 9:   System.out.println(“三季度”);   break;   ase 10:   case 11:   ase 12:   System.out.println(“四季度”);   break;   default:   System.out.println(“不是有效的月份”);  }  }  }   上述程序运行后的结果是哪项?()  

    • A、一季度
    • B、二季度
    • C、三季度
    • D、四季度

    正确答案:B

  • 第13题:

    关于JAVA初级的代码计算问题

    下面的方法,当输入为2的时候返回值是多少?         

    public int getValue(int i) {       

     int result = 0;       

      switch (i) { 

                case 1: 

                    result = result + i;             

    case 2: 

                    result = result + i * 2;             

    case 3: 

                    result = result + i * 3;         } 

                return result;     } 


    答案:10

    当i=2 case 1 不成立 case 2 时 result = 0 + 2 * 2 result = result + i * 2;后面你没有写break; 所以result = result + i * 2;执行结束后不会停止接着执行 result = 4 + 2 * 3

     

  • 第14题:

    以下程序段的运行结果是( )。 include main() {int x=2,y=1: switch(x) {case 1: switch

    以下程序段的运行结果是( )。 include<stdio.h> main() {int x=2,y=1: switch(x) {case 1: switch(y) {case 0:printf("x=2,y=1\n");break; case 1:printf("y=1\n");break; } case 2:printf("x=2\n"); } }


    正确答案:x=2
    x=2

  • 第15题:

    若有以下定义,则正确的swish语句是______。 float x;int a,b;

    A.switch(x) {case 1.0:printf("*\n"); case 2.0:printf("**\n"); }

    B.switch(x) {case 1,2:printf("*\n"); case 3:printf("**\n"); }

    C.switch(a+b) {case 1:printf("\n"); case 1+2:printf("**\n"); }

    D.switch(a+b); {case 1:printf("*\n"); case 2:printf("**\n"); }


    正确答案:C
    解析:switch后必须是整型或字符型的表达式,因此选项A、B错误;表达式的括号外没有分号,因此选项D错误。

  • 第16题:

    若a,b,c1,c2,x,y均是整型变量,正确的switch语句是______。

    A.switch(a+b); {case 1:y=a+b;break; case 0:y=a-b;break; }

    B.switch(a*a+b*b) {case 3; case 1:y=a+b;break; case 3:y=b-a;break; }

    C.switch a {case c1:y=a-b;break; case c2;x=a*b;break; default:x=a+b; }

    D.switch(a-b) {default:y=a*b;break; case 3:case 4:x=a+b;break; case 10:case11:y=a-b;break; }


    正确答案:D
    解析:由switch语句的格式排除A、C项,选项B中出现了两个相同的case标号。

  • 第17题:

    给出下面代码片段: public class Test{ public static void main (String args[ ]){ int m; switch(m) { case 0:System.out.println("case 0"); case 1:System.out.println("case 1"):break; case 2: default:System.out.println("default"); } } } 下列m的( )值将引起"default"的输出。

    A.1

    B.2

    C.4

    D.0


    正确答案:B

  • 第18题:

    执行如下程序: public class Test { public static void main (String args[]) { int x=1,a=0,b=0; switch (x) { case 0: b++; case 1: a++; case 2: a++;b++; } System.out.println("a=" +a ",b=" +b); } } 该程序的输出结果是( )。

    A.a=2,b=1

    B.a=1,b=1

    C.a=1,b=0

    D.a=2,b=2


    正确答案:A
    解析:本题关键是要搞清楚该程序执行了哪几条语句。由于x的值等于1,所以switch结构中,程序从case1后面的语句开始执行,又因为case1后面的语句没有以break结束,所以程序要继续往下执行case 2后面的语句。所以,该程序共执行了这3条语句:a++;a++;b++;因此,变量a和b最后的值应该为2和1。

  • 第19题:

    给出下列的程序段,m为何值时将会输出default? ( ) switch(m) { case 0: System.out.println("case 0"); case 1:System.out.println("case 1");break; case 2:System.out.println("case 2");break; default:System.out.println("default"); }

    A.0

    B.1

    C.2

    D.3


    正确答案:D

  • 第20题:

    若有定义float x=1.5;int a=1,b=3,c=2;,则正确的switch语句是

    A.swimh(x) {case 1.0:printf("*\n"); case 2.0:printf("**\n");}

    B.switch((int)x); {case 1:printf("*\n"); case 2:printf("**\n");}

    C.switch(a+

    D. {case 1:pfintf("*\n"); case 2+1:pfintf("**\n");}switch(a-I-{case 1:pfintf("*\n"); case c:printf("**\n");}


    正确答案:C
    解析: C语言中,switch语句专用于实现多分支结构程序,其特点是各分支清晰且直观。switch后面括号中可以是任何表达式,取其整数部分与各常量表达式进行比较。常量表达式中不能出现变量,且类型必须是整型、字符型或枚举型,各常量表达式各不相同。

  • 第21题:

    给出下列的程序代码片段,m为哪个值时将会输出default? switch(m) { case 0:System.out.println("case0"); case 1:System.out.println("case 1");break; case 2:System.out.println("case 2");break; default:System.out.println("default"); }

    A.0

    B.1

    C.2

    D.3


    正确答案:D
    解析:此题考查switch语句的用法,switch的判断的条件必须是一个int型值,也可以是byte,short和char型的值,case中需要注意的是一个case后面一般要接一个break语句才能结束判断,否则将继续执行其他case而不进行任何判断,如果没有任何值符合CaSe列出的判断,则执行default的语句,default是可选的,可以没有,如果没有default而又没有任何值匹配case中列出的值,则switch不执行任何语句。

  • 第22题:

    若定义:float x;int a,b;,则正确的switch语句是( )。

    A.switch(x) { case1.0:cout<<"*\n"; case2.0:cout<<"**\n";

    B.switch(x) { case 1.2:cout<<"*\n"; case 3:cout<<"**\n"; }

    C.switch(a+b) { case 1.0:cout<<"*\n"; case 1+2:cout<<"**\n ";

    D.switch(a+b) { case 1:cout<<"*\n"; case 2:cout<<"**\n";


    正确答案:D

  • 第23题:

    研究下面的Java代码:  switch (x) {  case 1:  System.out.println(1);  case 2:  case 3:  System.out.println(3);  case 4:  System.out.println(4); }  当x=2时,运行结果是()。 

    • A、没有输出任何结果
    • B、输出结果为3
    • C、输出结果是3和4
    • D、输出结果是1、3和4

    正确答案:C