选择语句+循环语句作业+答案 您所在的位置:网站首页 break语句只能用在循环体内和switch语句体内 选择语句+循环语句作业+答案

选择语句+循环语句作业+答案

2023-09-06 16:08| 来源: 网络整理| 查看: 265

一、填空题 1.Java中有两种类型的选择结构的控制语句,分别是if语句和________。

2.在Java JDK1.7之前,switch只能支持byte、short、char、int或者其对应的封装类以及Enum类型。在JDK1.7中又加入了________类型。

3.for循环的语法格式是for (表达式1;表达式2;表达式3) {循环体},其中在整个循环过程中只执行一次的部分是________。

4.在循环结构中,如果想跳出循环体,结束整个循环结构可以使用 ________语句。

5.________语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。即只结束本次循环,而不是终止整个循环的执行。

6.使用Math.random( )返回带正号的 double值,该值大于等于0.0且小于1.0。使用该函数生成[30,60]之间的随机整数的语句是________。

二、选择题

以下代码的执行结果是( )。(选择一项) boolean m = false; if(m = false){ System.out.println(“false”); }else{ System.out.println(“true”); }

A. false B. true C. 编译错误 D. 无结果

分析如下Java代码,编译运行的输出结果是( )。(选择一项) public static void main(String[ ] args) { boolean a=true; boolean b=false; if (!(a&&b)) { System.out.print("!(a&&b)"); }else if (!(a||b)) { System.out.println("!(a||b)"); }else { System.out.println(“ab”); } }

A !(a&&b) B. !(a||b) C. ab D. !(a||b)ab

下列选项中关于变量x的定义,( )可使以下switch语句编译通过。(选择二项) switch(x) { case 100 : System.out.println(“One hundred”); break; case 200 : System.out.println(“Two hundred”); break; case 300 : System.out.println( “Three hundred”); break; default : System.out.println( “default”); }

A double x = 100; B. char x = 100; C. String x = “100”; D. int x = 100;

阅读下列文件定入的Java代码,其执行结果是( )。(选择一项) public class Test { public static void main(String[] args) { char ch = ‘c’; switch (ch) { case ‘a’: System.out.print(“a”); break; case ‘b’: System.out.print(“ab”); case ‘c’: System.out.print(“c”); default: System.out.print(“d”); } } }

A a B. b C. c D. cd

以下Java程序编译运行后的输出结果是( )。(选择一项) public class Test { public static void main(String[] args) { int i = 0, sum = 0; while (i if (i % 2 == 0) sum = sum + i; i++; }

A for (int x =1; x for(int i=0;i if (i= 0 && a = 90) {System.out.println("A级");} else if(a >= 80) {System.out.println("B级");} else if(a >= 70) {System.out.println("C级");} else if(a >= 60) {System.out.println("D级");} else if((a < 60) ) {System.out.println("E级");} } else {System.out.println("你输入的成绩有误");} } } public class Demo3 { public static void main(String[] args) { System.out.println("请输入一个成绩"); Scanner s = new Scanner(System.in); int a = s.nextInt(); String g; if(a >= 0 && a 0;i=i/2){ sum+=i%2*j; j*=10; } System.out.println(sum); } }

六、可选题

1.根据考试成绩输出对应的礼物,90分以上爸爸给买电脑,80分以上爸爸给买手机, 60分以上爸爸请吃一顿大餐,60分以下爸爸给买学习资料。 要求:该题使用多重if完成

public class Demo8 { public static void main(String[] args) { int score = 80; if (score >= 90) { System.out.println("电脑"); } else if (score >= 80) { System.out.println("手机"); } else if (score >= 60) { System.out.println("大餐"); } else { System.out.println("学习资料"); } } }

2.请根据英文单词的第一个字母判断星期几,如果第一个字母是一样的,则继续判断第二个字母。例如如果第一个字母是S,则继续判断第二个字母,如果第二个字母是a,则输出“星期六”。

public class Demo9 { public static void main(String[] args) { char i, j; System.out.println("please enter the first letter:"); Scanner input = new Scanner(System.in); String str = ""; str = input.next().toLowerCase(); // 转换成字符串 i = str.charAt(0); switch (i) { case 'm': System.out.println("Monday\n"); break; case 'w': System.out.println("Wednesday\n");break; case 'f': System.out.println("Friday\n"); break; case 't': System.out.println("please enter the next letter:"); str = input.next().toLowerCase(); j = str.charAt(0); if (j == 'u') System.out.println("Tuesday\n"); else if (j == 'h') System.out.println("Thursday\n"); else System.out.println("error\n"); break; case 's': System.out.println("please enter the next letter:"); // 转换成字符串 str = input.next().toLowerCase(); j = str.charAt(0); if (j == 'a') System.out.println("Saturday\n"); else if (j == 'u') System.out.println("Sunday\n"); else System.out.println("error\n"); break; default: System.out.println("error\n"); break; } } }

3.输入一批整数,使用循环求出最大值与最小值,输入0时结束。

public class Demo10 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int max = 0, min = 0; int nums = 1; //输入第一个数,指定它是最大值和最小值 System.out.println("请输入1个数:"); nums = input.nextInt(); max = nums; min = nums; //依次输入其他数字,并判断是否是最大值和最小值 for (int i = 2; nums != 0; i++) { System.out.println("请输入" +i + "个数:"); nums = input.nextInt(); if (nums != 0) { if (nums > max) { max = nums; } if (nums < min) { min = nums; } } } //输出最大值和最小值 System.out.println("最大值为:" + max); System.out.println("最小值为:" + min); } }

4.给20块钱买可乐,每瓶可乐3块钱,喝完之后退瓶子可以换回1块钱,问最多可以喝到多少瓶可乐。

public class Demo11 { public static void main(String[] args) { int money = 20, price = 3; // temp表示每一次购买的可乐瓶数 int temp = 0; // change表示每次购买完可乐剩下的钱 int change = 0; // sum表示总的可乐瓶数 int sum = 0; // 如果钱数大于可乐价格 while (money >= price) { // 购买可乐 temp = money / price; // 总可乐瓶数增加 sum += temp; // 计算买可乐剩下的钱 change = money % price; // 兑换可乐瓶子,计算剩余的总钱数 money = temp + change; } // 输出结果 System.out.println(sum); } }

5.从键盘输入某个十进制小数,转换成对应的二进制小数并输出。

public class Demo12 { public static void main(String[] args) { System.out.println("请输入一个10进制的小数:"); Scanner sc = new Scanner(System.in); double d = sc.nextDouble(); double x = d; double sumx=0.0,k=0.1; int h = 1; for(;;k=k/10){ x=(x-(int)x)*2; sumx+=(int)x*k; h++;//记录小数位数 if(x-(int)x==0.0 || h==15){ break; } } System.out.println(sumx); } }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有