2022年java语言程序设计方案课后习题答案.docx
![资源得分’ 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)
《2022年java语言程序设计方案课后习题答案.docx》由会员分享,可在线阅读,更多相关《2022年java语言程序设计方案课后习题答案.docx(9页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精品学习资源习题 23使用“ = = ”对相同内容的字符串进行比较,看会产生什么样的结果;答:第一创建一个字符串变量有两种方式:String str = new Stringabc ;String str = abc ;Stringstr1=abc;Stringstr2=abc;使用“ = = ”会由于创建的形式不同而产生不同的结果:System.out.printlnstr1=str2 ; /true String str1 = new Stringabc ;Stringstr2=abc;System.out.printlnstr1=str2 ; /falseString str1 = ne
2、w Stringabc ;String str2 = new Stringabc ;Stringstr1=abc;Stringstr2=abc;System.out.printlnstr1=str2 ; /false 因此自符串假如是对内容进行比较,使用equals 方法比较牢靠;System.out.printlnstr1=str2 ; /true String str1 = new Stringabc ;Stringstr2=abc;System.out.printlnstr1.equalsstr2 ; /trueString str1 = new Stringabc ;Stringstr
3、2=newStringabc;System.out.printlnstr1.equalsstr2 ; /true5. 编写一个程序,把变量n 的初始值设置为 1678,然后利用除法运算和取余运算把变量的每位数字都提出来并打印,输出结果为: n=1678;n 的每位数字是 1, 6, 7, 8;如 n 为任意值呢?法一:public class Exercise5publicstaticvoidmainString argsint n=1678;int unit; int decimal;int hundred;int thousand;int count;thousand=n/1000;cou
4、nt=n%1000 ;hundred=count/100;count=count%100 ;decimal=count/10;count=count%10 ;unit=count;System.out.println1678包含的数字分别是: +thousand+,+hundred+,+decimal+,+unit;/ 假如 n 为任意值import java.io.*;public class Exercise51publicstaticvoidmainString args throws IOExceptionSystem.out.print请 输 入 一 个 整数: ;InputStre
5、amReader isStream=new InputStreamReaderSystem.in;BufferedReader bfReader=newBufferedReaderisStream;String input=bfReader.readLine;int length=input.length-1;int n=newIntegerinput.intValue;whilelength=0 int divisor=intMath.pow10,length;欢迎下载精品学习资源length=length-1;int output=n/divisor;n=n%divisor;System.
6、out.printoutput+,;法二: 建议使用 public class Exercise5publicstaticvoidmainString argsint n=1678;int unit; int decimal;int hundred;int thousand;thousand=n/1000%10 ;hundred=n/100%10 ;decimal=n/10%10 ;unit=n%10 ;System.out.println1678包含的数字分别是: +thousand+,+hundred+,+decimal+,+unit;/ 假如 n 为任意值import java.io.*
7、;public class Exercise51publicstaticvoidmainString args throws IOExceptionSystem.out.print请 输 入 一 个 整数: ;InputStreamReader isStream=new InputStreamReaderSystem.in;BufferedReader bfReader=newBufferedReaderisStream;String input=bfReader.readLine;int length=input.length-1;int n=newIntegerinput.intValue
8、;whilelength=0 int divisor=intMath.pow10,length;length=length-1;int output=n/divisor%10;System.out.printoutput+,;6. 编写 Java 程序,接受用户输入的 1-12之间的整数,如不符合条件就重新输入, 利用 switch 语句输出对应月份的天数;import java.io.* ;public class Exercise6publicstaticvoidmainString args throws IOExceptionint n;doSystem.out.print请输入 1-
9、12之间的整数: ;InputStreamReader isStream=new InputStreamReaderSystem.in;BufferedReader bfReader=newBufferedReaderisStream;String input=bfReader.readLine;n=new Integerinput.intValue;whilen12|n1;switchncase2:System.out.printlnn+月份 29 天 ; break ;case 1:case 3:case 5:case 7:case 8:case 10:case12:System.out.
10、printlnn+月份 31 天 ; break ;case 4:case 6:欢迎下载精品学习资源case 9:case11:System.out.printlnn+月份 30 天 ;break ; 7. 编写Java 程序运算小于一个整数的全部素数并输出;import java.io.*;public class Exercise7publicstaticvoidmainString args throws IOExceptionSystem.out.print请 输 入 一 个 整数: ;InputStreamReader isStream=new InputStreamReaderSy
11、stem.in;BufferedReader bfReader=new BufferedReaderisStream;Stringinput=bfReader.readLine;int n=newIntegerinput.intValue;int i;System.out.println2;fori=2;in ;i+forint j=2;j=i/2+1; j+ if i%j=0 break;if j=i/2+1 System.out.printlni; 9. 编写Java 程序实现:输入一组整数存放在数组中,比较并输出其中的最大值和 最小值;在将数组元素从小到大排序并输 出;import ja
12、va.io.*;public class Exercise9publicstaticvoidmainStringInputStreamReader isStream=new InputStreamReaderSystem.in;BufferedReader bfReader=newBufferedReaderisStream;String input=bfReader.readLine;int n=new Integerinput.intValue;intArrayi=n;/求最大最小值int max=intArray0;int min=intArray0;forint j=0;jN ; j+
13、 ifmaxintArrayj min=intArrayj;最 大值为:最 小值为:System.out.println +max ;System.out.println +min ;/从小到大排序int temp;forint i=0;iN ; i+forint j=i;jintArrayj temp=intArrayi;intArrayi=intArrayj;intArrayj=temp;/将排序后的结果打印欢迎下载精品学习资源args throws IOExceptionSystem.out.println排序 后 的 数 组final int N=5;为: ;int intArray=
14、new intN;forint i=0;iN ; i+/数组的赋值forint i=0;iN ;i+System.out.printintArrayi+,;System.out.print数 组 的第+i+个元素是: ;欢迎下载精品学习资源10. 编写一个方法来运算正方形的面积和周长;import java.io.*;public class Exercise10System.out.print“请输入正方形的边长:” InputStreamReader isStream=new InputStreamReaderSystem.in;BufferedReader bfReader=new Bu
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022 java 语言程序设计 方案 课后 习题 答案
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内