为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

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

2019-05-11 25页 doc 49KB 143阅读

用户头像

is_624976

暂无简介

举报
java语言程序设计课后习题答案习题2 3.使用“= =”对相同内容的字符串进行比较,看会产生什么样的结果。 答:首先创建一个字符串变量有两种方式: String str = new String("abc"); String str = "abc"; 使用“= =”会因为创建的形式不同而产生不同的结果: String str1 = "abc"; String str2 = "abc"; System.out.println(str1= =str2); //true String str1 = new String("abc"); Str...
java语言程序设计课后习题答案
习题2 3.使用“= =”对相同内容的字符串进行比较,看会产生什么样的结果。 答:首先创建一个字符串变量有两种方式: String str = new String("abc"); String str = "abc"; 使用“= =”会因为创建的形式不同而产生不同的结果: String str1 = "abc"; String str2 = "abc"; System.out.println(str1= =str2); //true String str1 = new String("abc"); String str2 = "abc"; System.out.println(str1= =str2); //false String 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); //true String str1 = new String("abc"); String str2 = "abc"; System.out.println(str1.equals(str2)); //true String str1 = new String("abc"); String str2 = new String("abc"); System.out.println(str1.equals(str2)); //true 5.编写一个程序,把变量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; 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{ 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); length=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包含的数 字分别是: "+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 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.*; public 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(); }while(n>12||n<1); 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{ public 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=2;iintArray[j]) min=intArray[j]; } System.out.println("最大值为: "+max); System.out.println("最小值为: "+min); //从小到大排序 int temp; for(int i=0;iintArray[j]){ temp=intArray[i]; intArray[i]=intArray[j]; intArray[j]=temp; } } } //将排序后的结果打印 System.out.println("排序后的数组为: "); for(int i=0;imax) max=array[i][j]; }} System.out.println("4*4数组的最大值"+max); } } 15.创建一个程序把输入字符串的大小互 换。 import java.io.*; public class Exercise15{ public 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(); char[] change=input.toCharArray(); for(int i=0;i
整数时该怎么办? import java.io.*; public class Exercise17{ public static void main(String[] args) throws NumberFormatException{ int[] array=new int[args.length]; for(int i=0;i
/
本文档为【java语言程序设计课后习题答案】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索