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

  • 格式:doc
  • 大小:36.50 KB
  • 文档页数:7

下载文档原格式

  / 7
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

习题2

3.使用“= =”对相同内容的字符串进行比较,看会产生什么样的结果。

答:首先创建一个字符串变量有两种方式:String str = new String("abc");

String str = "abc";

使用“= =”会因为创建的形式不同而产生不同的结果:

String str1 = "abc";

String str2 = "abc";

=str2); ;

public class Exercise51{

public static void main(String[] args) throws IOException{

"请输入一个整数:");

InputStreamReader isStream=new InputStreamReader;

BufferedReader bfReader=new BufferedReader(isStream);

String input=();

int length=()-1;

int n=new

Integer(input).intValue();

while(length>=0){

int divisor=(int) (10,length); length=length-1; int output=n/divisor;

n=n%divisor;

",");

}

}

}

法二:(建议使用)

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;

"1678包含的数字分别是:

"+thousand+','+hundred+','+decimal+', '+unit);

}

}

;

public class Exercise51{

public static void main(String[] args) throws IOException{

"请输入一个整数:");

InputStreamReader isStream=new InputStreamReader;

BufferedReader bfReader=new BufferedReader(isStream);

String input=();

int length=()-1;

int n=new

Integer(input).intValue();

while(length>=0){

int divisor=(int) (10,length); length=length-1;

int output=n/divisor%10;

",");

}

}

}

6.编写Java程序,接受用户输入的1-12之间的整数,若不符合条件则重新输入,利用switch语句输出对应月份的天数。

import .*;

public class Exercise6{

public static void main(String[] args) throws IOException{

int n;

do{

"请输入1-12之间的整数:");

InputStreamReader isStream=new InputStreamReader;

BufferedReader bfReader=new BufferedReader(isStream);

String input=();

n=new Integer(input).intValue(); }while(n>12||n<1);

switch(n){

case 2: "月份29天");break;

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12: "月份31天");break;

case 4:

case 6:

case 9:

case 11: "月份30天");break;

}

} }

7.编写Java程序计算小于一个整数的全部素数并输出。

import .*;

public class Exercise7{

public static void main(String[] args) throws IOException{

"请输入一个整数:");

InputStreamReader isStream=new InputStreamReader;

BufferedReader bfReader=new BufferedReader(isStream);

String input=();

int n=new

Integer(input).intValue();

int i;

for(i=2;i

for(int j=2;j<=i/2+1;j++){

if (i%j==0) break;

if (j==i/2+1) } }

}}

9.编写Java程序实现:输入一组整数存放在数组中,比较并输出其中的最大值和最小值;在将数组元素从小到大排序并输出。import .*;

public class Exercise9{

public static void main(String[] args) throws IOException{

final int N=5;

int[] intArray=new int[N];

ntValue();

intArray[i]=n;

}

;

public class Exercise10{

请输入正方形的边长:”)

InputStreamReader isStream=new InputStreamReader;

BufferedReader bfReader=new BufferedReader(isStream);

String input=();

int bianChang=(input);

"周长为:"+

bianChang*4);

"面积为:"+

bianChang*bianChang);

}

11.编写一个方法来计算1000以内的素数之和并输出。

public class Exercise11{

public static void main(String[] args){

int sum=2;