java语言程序设计课后习题内容答案

上传人:不*** 文档编号:93090688 上传时间:2019-07-16 格式:DOC 页数:7 大小:62KB
返回 下载 相关 举报
java语言程序设计课后习题内容答案_第1页
第1页 / 共7页
java语言程序设计课后习题内容答案_第2页
第2页 / 共7页
java语言程序设计课后习题内容答案_第3页
第3页 / 共7页
java语言程序设计课后习题内容答案_第4页
第4页 / 共7页
java语言程序设计课后习题内容答案_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《java语言程序设计课后习题内容答案》由会员分享,可在线阅读,更多相关《java语言程序设计课后习题内容答案(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= =str2); /falseString str1

2、= 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.println(str1.equals(str2); /trueString str1 = new

3、 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 Exercise5 public static void main(String args) int n=1678; int unit; int decimal; int hundred; int thousand;

4、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 Exercise51 public static void main(String args) throws IOException

5、System.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(length=0) int divisor=(int) Math.pow(10,length); le

6、ngth=length-1; int output=n/divisor; n=n%divisor; System.out.print(output+,); 法二:(建议使用)public class Exercise5 public 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%10; unit=n%10; System.out.println(1678包含

7、的数字分别是:+thousand+,+hundred+,+decimal+,+unit); /如果n为任意值import java.io.*;public class Exercise51 public static void main(String args) throws IOException System.out.print(请输入一个整数:); InputStreamReader isStream=new InputStreamReader(System.in); BufferedReader bfReader=new BufferedReader(isStream); String

8、 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之间的整数,若不符合条件则重新输入,利用switch语句输出对应月份的天数。import java.io.*;publ

9、ic class Exercise6 public static void main(String args) throws IOException int n; do System.out.print(请输入1-12之间的整数:); InputStreamReader isStream=new InputStreamReader(System.in); BufferedReader bfReader=new BufferedReader(isStream); String input=bfReader.readLine(); n=new Integer(input).intValue();

10、while(n12|n1); switch(n) case 2: System.out.println(n+月份29天);break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: System.out.println(n+月份31天);break; case 4: case 6: case 9: case 11: System.out.println(n+月份30天);break; 7编写Java程序计算小于一个整数的全部素数并输出。import java.io.*;public class Exercise7 publi

11、c static void main(String args) throws IOException System.out.print(请输入一个整数:); InputStreamReader isStream=new InputStreamReader(System.in); BufferedReader bfReader=new BufferedReader(isStream); String input=bfReader.readLine(); int n=new Integer(input).intValue(); int i; System.out.println(2); for(i

12、=2;in;i+) for(int j=2;j=i/2+1;j+) if (i%j=0) break; if (j=i/2+1) System.out.println(i); 9编写Java程序实现:输入一组整数存放在数组中,比较并输出其中的最大值和最小值;在将数组元素从小到大排序并输出。import java.io.*;public class Exercise9 public static void main(String args) throws IOException final int N=5; int intArray=new intN; /数组的赋值 for(int i=0;iN

13、;i+) System.out.print(数组的第+i+个元素是:); InputStreamReader isStream=new InputStreamReader(System.in); BufferedReader bfReader=new BufferedReader(isStream); String input=bfReader.readLine(); int n=new Integer(input).intValue(); intArrayi=n; /求最大最小值 int max=intArray0; int min=intArray0; for(int j=0;jN;j+) if(maxintArrayj) min=intArrayj; System.out.println(最大值为:+max); System.out

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 教育学

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号