没有合适的资源?快使用搜索试试~ 我知道了~
教材第二章编程练习题 2.6 题/*** Calculate the sum of the digits of a number between 0 and10
资源详情
资源评论
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/86297286/bg1.jpg)
第 2-3 章作业
一、 填空题
1. Java 语言中有 4 种基本的整数类型,哪种类型所占的内存空间最小,写出定义该
类型的关键字_____byte________。
2. Java 语言中有 4 种基本的整数类型,哪种类型所占的内存空间最大,写出定义该
类型的关键字______double_______。
3. Java 中存在一种基本的数据类型,该类型定义的变量不能与其他类型转换,定义
该类型用____boolean_______。
4. 布尔型定义的成员变量是有默认值的,它的值是___false______。
5. 定义变量保存含有小数的数据时,使用__double___定义的变量精度比较高。
6. Java 源程序经过编译后生成被称为____字节码_____的特殊机器语言码,然后经
过___JVM______解释运行。
7. 声明一个值为 3.14 的 double 型常量 PI 的语句为
_____final double PI = 3.14;_________________。
8. 下列程序中,首先声明和初始化三个变量 ch = ‘a’,变量 d=0.1,变量 l =12L,
并将该三个变量打印输出.请将程序补充完整.
public class Assign{
public static void main(String args[]){
___char___ _ ch = ‘a’;
___double____ d = 0.1;
___long___ _ l = 12L;
System.out.println("ch=" + _ch___);
System.out.println("d=" + _d___);
System.out.println("l=" + _l___);
}
}
9. 执行下列语句后,变量 x,y 的值分别为____0,1___________,原因是
对于&&和||符号,如果左式已经决定整个式子的结果,则不再判断右式;对于
&和|符号,无论情况如何,都将执行两边的式子。
int x = 0, y = 0;
System.out.println( ((x > 1) && (++x == 0)) + " " + x);
System.out.println( ((y < 1) | (y++ == 0)) + " " + y);
![](https://csdnimg.cn/release/download_crawler_static/86297286/bg2.jpg)
10.阅读下面代码
System.out.print("Please input your choice[1,2]:");
long i = new Scanner(System.in).nextLong();
switch (i){
case 1 :
System.out.println("Your choice is 1");
break;
case 2 :
System.out.println("Your choice is 2");
break;
default:
System.out.println("Wrong choice");
}
以上代码错误的地方是:
switch 语句中不能使用 long 类型变量作为判断条件
二、单项选择题
1.以下说法正确的是__A____。
(A) Java 中所有的方法都必须在类内定义
(B) Java 中主方法可以不在类内定义,其他方法都必须定义在类内
(C) Java 中主方法必须定义在类内,其他方法可以不必定义在类内
(D) Java 中所有方法都不必在类内定义
2. Java 源文件和编译后的文件扩展名分别是___C____。
(A).class 和 .java (B).class 和 .class
(C).java 和 .class (D).java 和 .java
3. 关于布尔类型说法正确的是 ___A_____。
(A)boolean 表示布尔类型,它的取值只有 true 和 false
(B)bool 表示布尔类型,它的取值只有 true 和 false
(C)boolean 表示布尔类型,它的取值只有 1 和 0
(D)bool 表示布尔类型,它的取值只有 1 和 0
![](https://csdnimg.cn/release/download_crawler_static/86297286/bg3.jpg)
4. 下面表达式错误的是___B_____。
(A)int i = 100;
(B)float f = 100.0;
(C)Object o = “Hello world”;
(D)char c = ‘\u1234’;
5. 下面代码的输出是___A_____。
class C {
public static void main (String [] args) {
int x = 1;
System.out.print((x > 1) & (x++ > 1));
System.out.print(“ ”);
System.out.print((x > 1) && (x++ > 1));
}
}
(A)false true
(B)true false
(C)false false
(D)true true
6. 下列叙述中正确的是_____A____。
(A) Java 语言的标识符是区分大小写的
(B)Java 源程序文件名可以任意命名
(C)Java 源程序文件的扩展名为.jar
(D) 一个 Java 源程序文件里 public 类的数目不限
7. 下列标识符中,合法的是___A______。
(A)_name (B) 4Person
(C)public (D) -3.1415
三、编程题
教材第二章编程练习题 2.6 题
package course.ch2;
import java.util.Scanner;
/**
* Calculate the sum of the digits of a number between 0 and
1000
![](https://csdnimg.cn/release/download_crawler_static/86297286/bg4.jpg)
*/
public class sumofDigits {
/**
* The entrance of program
* @param args: command line arguments
*/
public static void main(String[] args){
int num,sum=0;
System.out.println("Enter a number between 0 and
1000:");
num=new Scanner(System.in).nextInt();
for(;num>0;num/=10)
sum+=num%10;
System.out.println("The sum of the digits is "+sum);
}
}
![](https://csdnimg.cn/release/download_crawler_static/86297286/bg5.jpg)
第 4-5 章作业
一、 填空题
1:假设
String s1 = "Welcome to Java";
String s2 = s1;
String s3 = new String("Welcome to Java");
那么下面表达式的结果是什么?
(1) s1 == s2 __________true_______________
(2) s1 == s3 __________false__________________
(3) s1.equals(s2) __________true_________________
(4) s2.equals(s3) __________true__________________
(5) s1.compareTo(s2); __________0__________________
(6) s2.compareTo(s3); __________0_____________________
(7) s1.charAt(0); __________’W’_____________________
(8) s1.indexOf('j'); __________-1______________________
(9) s1.indexOf("to"); __________8______________________
(10) s1.lastIndexOf("o",15) _________9______________________
(11) s1.substring(3, 11); __________”come to ”_______________
(12) s1.endsWith("Java") ______true______________________
(13) s1.startsWith("wel"); _________false__________________
(14) " We come ".trim(); _________”We come”__________________
(15) s1.toUpperCase(); _________”WELCOME TO JAVA”____
(16) s1.replace('o', 'T'); _________”WelcTme tT Java”_________
2.如果
StringBuffer s1 = new StringBuffer("Java");
StringBuffer s2 = new StringBuffer("HTML");
假设下列每个语句是独立的,每条语句结束后,写出相应结果
(1) s1.append(" is fun"); s1 为_”Java is fun”___
(2) s1.append(s2); s1 为_”JavaHTML”_______
(3) s1.insert(2, "is fun"); s1 为_”Jais funva”______
(4) s1.insert(1,s2); s1 为_”JHTMLava”_________
(5) char c = s1.charAt(2); c 为__’v’___________
(6) int i = s1.length(); i 为___4________________
(7) s1.deleteCharAt(3); s1 为__”Jav”________________
(8) s1.delete(1,3); s1 为__”Ja”_________________
(9) s1.reverse(); s1 为__”avaJ”______________
(10) s1.replace(1,3, "Computer"); s1 为__”JComputera”____________
(11) String s3 = s1.substring(1,3);
s3 为___”av”____________,s1 为____”Java”_______________
(12) String s4 = s1.substring(2);
剩余84页未读,继续阅读
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![avatar](https://profile-avatar.csdnimg.cn/9ceabaa8387c434eb682d2a55e0eec78_weixin_35778603.jpg!1)
周林深
- 粉丝: 54
- 资源: 290
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
评论0