java语言程序设计课后习题内容答案.doc
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《java语言程序设计课后习题内容答案.doc》由会员分享,可在线阅读,更多相关《java语言程序设计课后习题内容答案.doc(7页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、习题 23使用“= =”对相同内容的字符串进行 比较,看会产生什么样的结果。 答:首先创建一个字符串变量有两种方式:String str = new String(“abc“); String str = “abc“; 使用“= =”会因为创建的形式不同而产生 不同的结果: String str1 = “abc“; String str2 = “abc“; System.out.println(str1= =str2); /trueString str1 = new String(“abc“); String str2 = “abc“; System.out.println(str1= =st
2、r2); /falseString str1 = new String(“abc“); String str2 = new String(“abc“); System.out.println(str1= =str2); /false 因此自符串如果是对内容进行比较,使用 equals 方法比较可靠。 String str1 = “abc“; String str2 = “abc“; System.out.println(str1= =str2); /trueString str1 = new String(“abc“); String str2 = “abc“; System.out.prin
3、tln(str1.equals(str2); /trueString str1 = new String(“abc“); String str2 = new String(“abc“); System.out.println(str1.equals(str2); /true5编写一个程序,把变量 n 的初始值设置 为 1678,然后利用除法运算和取余运算把 变量的每位数字都提出来并打印,输出结 果为:n=1678。n 的每位数字是 1,6,7,8。若 n 为任意值呢? 法一:public class Exercise5public static void main(String args)in
4、t n=1678;int unit;int decimal;int hundred;int thousand;int count;thousand=n/1000;count=n%1000;hundred=count/100;count=count%100;decimal=count/10;count=count%10;unit=count;System.out.println(“1678 包含的数 字分别是:“+thousand+,+hundred+,+decimal+, +unit); /如果 n 为任意值import java.io.*; public class Exercise51pu
5、blic static void main(String args) throws IOExceptionSystem.out.print(“请输入一个整数:“);InputStreamReader isStream=new InputStreamReader(System.in);BufferedReader bfReader=new BufferedReader(isStream);String input=bfReader.readLine();int length=input.length()-1;int n=new Integer(input).intValue();while(le
6、ngth=0) int divisor=(int) Math.pow(10,length); length=length-1;int output=n/divisor;n=n%divisor;System.out.print(output+“,“); 法二:(建议使用)public class Exercise5public static void main(String args)int n=1678;int unit;int decimal;int hundred;int thousand;thousand=n/1000%10;hundred=n/100%10;decimal=n/10%1
7、0;unit=n%10;System.out.println(“1678 包含的数 字分别是:“+thousand+,+hundred+,+decimal+, +unit); /如果 n 为任意值import java.io.*; public class Exercise51public static void main(String args) throws IOExceptionSystem.out.print(“请输入一个整数:“);InputStreamReader isStream=new InputStreamReader(System.in);BufferedReader bf
8、Reader=new BufferedReader(isStream);String input=bfReader.readLine();int length=input.length()-1;int n=new Integer(input).intValue();while(length=0) int divisor=(int) Math.pow(10,length); length=length-1;int output=n/divisor%10;System.out.print(output+“,“);6.编写 Java 程序,接受用户输入的 1-12 之间的整数,若不符合条件则重新输入
9、, 利用 switch 语句输出对应月份的天数。import java.io.*; public class Exercise6public static void main(String args) throws IOExceptionint n;doSystem.out.print(“请输入 1-12 之 间的整数:“);InputStreamReader isStream=new InputStreamReader(System.in);BufferedReader bfReader=new BufferedReader(isStream);String input=bfReader.r
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 语言程序设计 课后 习题 内容 答案
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内