选择语句+循环语句作业
一、 填空题
- Java中有两种类型的选择结构的控制语句,分别是if语句和 switch 。
- 在Java JDK1.7之前,switch只能支持byte、short、char、int或者其对应的封装类以及Enum类型。在JDK1.7中又加入了 String 类型。
- for循环的语法格式是for (表达式1;表达式2;表达式3) {循环体},其中在整个循环过程中只执行一次的部分是表达式1 。
- 在循环结构中,如果想跳出循环体,结束整个循环结构可以使用 break 语句。
- ___continue__语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。即只结束本次循环,而不是终止整个循环的执行。
二、 选择题
1. |
以下代码的执行结果是( B)。(选择一项) |
|
|
boolean m = false; if(m = false){ System.out.println("false"); }else{ System.out.println("true"); } |
|
|
|
|
|
A. |
false |
|
B. |
true |
|
C. |
编译错误 |
|
D. |
无结果 |
2. |
分析如下Java代码,编译运行的输出结果是(A )。(选择一项) |
|
|
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 |
3. |
下列选项中关于变量x的定义,(D )可使以下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; |
4. |
阅读下列文件定入的Java代码,其执行结果是(D )。(选择一项) |
|
|
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 |
5. |
以下Java程序编译运行后的输出结果是( B )。(选择一项) |
|
|
public class Test { public static void main(String[] args) { int i = 0, sum = 0; while (i <= 10) { sum += i; i++; } System.out.println(sum); } } |
|
|
|
|
|
A |
0 |
|
B. |
55 |
|
C. |
50 |
|
D. |
36 |
6. |
以下四个选项中和下面代码功能相同的是(B )。(选择一项) |
|
|
int i = 1; int sum = 0; while (i <= 100) { if (i % 2 == 0) sum = sum + i; i++; } |
|
|
|
|
|
A |
for (int x =1; x<=100;x++){ sum=sum+x;} |
|
B. |
for (int x =0; x<=100;x+=2){ sum=sum+x;} |
|
C. |
for (int x =1; x<=100;x+=2){ sum=sum+x;} |
|
D. |
上述全对 |
7. |
以下do-while循环代码的执行结果是( A )。(选择一项) |
|
|
int a=0; int c=0; do{ --c; a=a-1; }while(a>0); System.out.println(a+" "+c); |
|
|
|
|
|
A. |
-1 -1 |
|
B. |
死循环 |
|
C. |
-1 -2 |
|
D. |
-1 0 |
8. |
while循环和do-while循环的区别是( D)。(选择一项) |
|
|
|
|
|
A. |
没有区别,这两个结构在任何情况下效果一样 |
|
B. |
while循环比do-while循环执行效率高 |
|
C. |
while循环是先循环后判断,所以循环体至少被执行一次 |
|
D. |
do-while循环是先循环后判断,所以循环体至少被执行一次 |
9. |
在Java中有如下代码,则编译运行该类的输出结果是( D)。(选择一项) |
|
|
public static void main(String[ ] args) { for(int i=0;i<10;i++){ if (i%2!=0) return; System.out.print(i); } } |
|
|
|
|
|
A |
13578 |
|
B. |
02468 |
|
C. |
0123456789 |
|
D. |
0 |
10. |
下面程序执行的结果是在屏幕上打印( B )次Java基础班。(选择一项) |
|
|
for(int i=1;i<=10;i++){ if (i<5) continue; System.out.println("Java基础班"); } |
|
|
|
|
|
A |
5 |
|
B. |
6 |
|
C. |
7 |
|
D. |
8 |
三、 判断题(共20个题目,总计10分)
- if语句的条件表达式的结果都必须是boolean值。(√ )
- switch选择语句是多分支选择语句,只能处理等值条件判断的情况,表达式可以是int类型、char类型,但不能是double,float类型。( √)
- while循环结构的特点是先循环再判断,循环体至少执行一次。(× )
- for循环的语法格式是for (表达式1;表达式2;表达式3) {循环体},其中三个表达式都可以省略。( √ )
- break语句可以出现在switch语句和循环语句中。( √ )
- continue语句可以出现在switch语句和循环语句中。( × )
四、 简答题
- if多分支语句和switch语句的异同之处
都可以进行多次判断,if可以判断逻辑运算,算术运算等只要结果是boolean类型就行,switch的case只能是常量。
- while和do-while语句的异同之处
都是根据条件表达式进行循环,do-while循环语句在前,所以无论条件表达式是否满足,循环体都会至少执行一次。
- break和continue语句的作用
break是终止本次循环并跳出循环,continue是终止本次循环进行下一次循环。
五、 编码题
- 输入一个数,判断是奇数还是偶数
Import java.util.Scanner;
class Task51 {
public static void main(String[] args) {
//1. 输入一个数,判断是奇数还是偶数
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数");
int i = sc.nextInt();
if (i % 2 == 0) {
System.out.println("该数是偶数");
}else
System.out.println("该数是奇数");
}
}
- 根据成绩输出对应的等级,使用if多分支和switch语句分别实现。
a) A级 [90,100]
b) B级 [80,90)
c) C级 [70,80)
d) D级 [60,70)
e) E级 [0,60)
import java.util.Scanner;
class Task52 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个成绩");
int score = sc.nextInt();
if (score <= 100 && score >= 90) {
System.out.println("该生成绩为A级");
}else if (score <= 89 && score >= 80) {
System.out.println("该生成绩为B级");
}else if (score <= 79 && score >= 70) {
System.out.println("该生成绩为C级");
}else if (score <= 69 && score >= 60) {
System.out.println("该生成绩为D级");
}else if ((score <= 59 && score >= 0)) {
System.out.println("该生成绩为E级");
}else
System.out.println("你输入的成绩有误");
}
}
import java.util.Scanner;
class Task52 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个0-100的成绩");
int score = sc.nextInt();
int i = score / 10;
switch (i) {
case 10:
case 9:
System.out.println("该生成绩为A级");
break;
case 8:
System.out.println("该生成绩为B级");
break;
case 7:
System.out.println("该生成绩为C级");
break;
case 6:
System.out.println("该生成绩为D级");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
System.out.println("该生成绩为E级");
break;
default:
System.out.println("你输入的成绩有误");
}
}
}
- 根据月份,输出对应的季节,并输出至少两个描述该季节的成语和活动。
import java.util.Scanner;
class TestSeason {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入月份");
int month = sc.nextInt();
switch (month) {
case 1:
case 2:
case 3:
System.out.println("春天-春暖花开 春意盎然-放鞭炮 春游");
break;
case 4:
case 5:
case 6:
System.out.println("夏天-夏日炎炎 夏雨雨人-游泳 赛龙舟");
break;
case 7:
case 8:
case 9:
System.out.println("秋天-秋高气爽 五谷丰登-放风筝 割麦子");
break;
case 10:
case 11:
case 12:
System.out.println("冬天-冰天雪地 大雪纷纷-滑冰 堆雪人");
break;
default:
System.out.println("输入的季节有误");
}
}
}
- 从键盘输入一个班5个学生的分数,求和并输出。
import java.util.Scanner;
class TestScore {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个学生的成绩");
int a = sc.nextInt();
System.out.println("请输入第二个学生的成绩");
int b = sc.nextInt();
System.out.println("请输入第三个学生的成绩");
int c = sc.nextInt();
System.out.println("请输入第四个学生的成绩");
int d = sc.nextInt();
System.out.println("请输入第五个学生的成绩");
int e = sc.nextInt();
int sum = a + b + c + d + e;
System.out.println("这五个学生成绩和为" + sum);
}
}
六、 可选题
- 根据考试成绩输出对应的礼物,90分以上爸爸给买电脑,80分以上爸爸给买手机, 60分以上爸爸请吃一顿大餐,60分以下爸爸给买学习资料。
要求:该题使用多重if完成
import java.util.Scanner;
class TestGift {
public static void main(String[] args) {
/*1. 根据考试成绩输出对应的礼物,90分以上爸爸给买电脑,
80分以上爸爸给买手机, 60分以上爸爸请吃一顿大餐,
60分以下爸爸给买学习资料。*/
Scanner sc = new Scanner(System.in);
System.out.println("输入一个成绩");
int score = sc.nextInt();
if (score >= 90 && score <= 100) {
System.out.println("爸爸给买电脑");
}else if (score >= 80 && score < 90) {
System.out.println("爸爸给买手机");
}else if (score >= 60 && score < 80) {
System.out.println("爸爸请吃一顿大餐");
}else if (score < 60) {
System.out.println("爸爸给买学习资料");
}else
System.out.println("输入的成绩有误");
}
}
给20块钱买可乐,每瓶可乐3块钱,喝完之后退瓶子可以换回1块钱,问最多可以喝到多少瓶可乐。
class TestCola {
public static void main(String[] args) {
int s = 20;
int a = 0;
int b = 0;
int count = 0;
for (; ; ) {
if (s / 3 != 0) {
a = s / 3;
b = s % 3;
s = a + b;
count += a;
}else
break;
}
System.out.println("最多可以喝到" + count + "瓶");
}
}
一. 方法(函数)
1:函数的概念?函数的格式?格式的解释说明
完成特定功能的代码块。
修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2...){
方法体语句;
return 返回值;
}
修饰符:目前就用public static
返回值类型:就是功能结果的数据类型
方法名:符合命名规则即可
参数:
形式参数:就是方法定义的,用来接收参数
实际参数:实际参与运算的数据
参数类型:就是参数的数据类型
参数名:就是变量名
方法体语句:就是用来实现功能的代码
return:结束方法的
返回值:就是功能的运算结果,结果就是什么数据类型,返回值类型就是什么数据类型,
2:函数的调用
A:明确返回值类型的函数调用
输出调用,赋值调用
B:void类型的函数调用
单独调用
3:函数的练习:
A:求两个数据之和
public static int add(int a,int b) {
int sum = a + b;
return sum;
}
B:判断两个数据是否相等
public static boolean isEquals(int a,int b) {
return a == b;
}
C:获取两个数中较大的值
public static int getMax(int a,int b) {
int max = (a > b) ? a : b;
return max;
}
D:打印m行n列的星形矩形
import java.util.Scanner;
class printstar {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入行数");
int row = sc.nextInt();
System.out.println("输入列数");
int column = sc.nextInt();
printstar(row,column);
}
public static void printstar(int a,int b) {
for (int i = 1;i <= a ;i++ ) {
for (int j = 1;j <= b ;j++ ) {
System.out.println("*");
}
}
}
}
E:打印nn乘法表
import java.util.Scanner;
class print99 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入一个1-9的数");
int n = sc.nextInt();
print99(n);
}
public static void print99(int a ) {
for (int i =1;i <= a ;i++ ) {
for (int j = 1;j <= i ;j++ ) {
System.out.print(i + "*" + j + "=" + i * j + "\t");
}
System.out.println();
}
}
}
4:什么是函数重载?以及函数重载的练习?把讲过的案例练习一次即可
在同一个类中,方法名相同,参数列表不同。与返回值类型无关。
参数列表不同:
A:参数个数不同
B:参数类型不同
C:参数的顺序不同(算重载,但是在开发中不用)
===============================================================
===============================================================
二. 内存图
画图操作:
1.一个数组的内存图
2.两个数组的内存图
3.三个引用两个数组的内存图