java语言程序设计课后习题答案.docx
《java语言程序设计课后习题答案.docx》由会员分享,可在线阅读,更多相关《java语言程序设计课后习题答案.docx(11页珍藏版)》请在冰点文库上搜索。
java语言程序设计课后习题答案
习题2
3.使用“==”对相同内容的字符串进行比较,看会产生什么样的结果。
答:
首先创建一个字符串变量有两种方式:
Stringstr=newString("abc");
Stringstr="abc";
使用“==”会因为创建的形式不同而产生不同的结果:
Stringstr1="abc";
Stringstr2="abc";
=str2);;
publicclassExercise51{
publicstaticvoidmain(String[]args)throwsIOException{
"请输入一个整数:
");
InputStreamReaderisStream=newInputStreamReader;
BufferedReaderbfReader=newBufferedReader(isStream);
Stringinput=();
intlength=()-1;
intn=newInteger(input).intValue();
while(length>=0){
intdivisor=(int)(10,length);
length=length-1;
intoutput=n/divisor;
n=n%divisor;
",");
}
}
}
法二:
(建议使用)
publicclassExercise5{
publicstaticvoidmain(String[]args){
intn=1678;
intunit;
intdecimal;
inthundred;
intthousand;
thousand=n/1000%10;
hundred=n/100%10;
decimal=n/10%10;
unit=n%10;
"1678包含的数字分别是:
"+thousand+','+hundred+','+decimal+','+unit);
}
}
;
publicclassExercise51{
publicstaticvoidmain(String[]args)throwsIOException{
"请输入一个整数:
");
InputStreamReaderisStream=newInputStreamReader;
BufferedReaderbfReader=newBufferedReader(isStream);
Stringinput=();
intlength=()-1;
intn=newInteger(input).intValue();
while(length>=0){
intdivisor=(int)(10,length);
length=length-1;
intoutput=n/divisor%10;
",");
}
}
}
6.编写Java程序,接受用户输入的1-12之间的整数,若不符合条件则重新输入,利用switch语句输出对应月份的天数。
import.*;
publicclassExercise6{
publicstaticvoidmain(String[]args)throwsIOException{
intn;
do{
"请输入1-12之间的整数:
");
InputStreamReaderisStream=newInputStreamReader;
BufferedReaderbfReader=newBufferedReader(isStream);
Stringinput=();
n=newInteger(input).intValue();
}while(n>12||n<1);
switch(n){
case2:
"月份29天");break;
case1:
case3:
case5:
case7:
case8:
case10:
case12:
"月份31天");break;
case4:
case6:
case9:
case11:
"月份30天");break;
}
}}
7.编写Java程序计算小于一个整数的全部素数并输出。
import.*;
publicclassExercise7{
publicstaticvoidmain(String[]args)throwsIOException{
"请输入一个整数:
");
InputStreamReaderisStream=newInputStreamReader;
BufferedReaderbfReader=newBufferedReader(isStream);
Stringinput=();
intn=newInteger(input).intValue();
inti;
for(i=2;ifor(intj=2;j<=i/2+1;j++){
if(i%j==0)break;
if(j==i/2+1)}}
}}
9.编写Java程序实现:
输入一组整数存放在数组中,比较并输出其中的最大值和最小值;在将数组元素从小到大排序并输出。
import.*;
publicclassExercise9{
publicstaticvoidmain(String[]args)throwsIOException{
finalintN=5;
int[]intArray=newint[N];
ntValue();
intArray[i]=n;
}
;
publicclassExercise10{
请输入正方形的边长:
”)
InputStreamReaderisStream=newInputStreamReader;
BufferedReaderbfReader=newBufferedReader(isStream);
Stringinput=();
intbianChang=(input);
"周长为:
"+
bianChang*4);
"面积为:
"+
bianChang*bianChang);
}
11.编写一个方法来计算1000以内的素数之和并输出。
publicclassExercise11{
publicstaticvoidmain(String[]args){
intsum=2;
for(inti=2;i<=100;i++){
for(intj=2;j<=i/2+1;j++){
if(i%j==0)break;
if(j==i/2+1)}
}
}
}
12.使用数组存储一个英文句子“Javaisanobjectorientedprogramminglanguage.”显示该句子,并算出每个单词的平均字母数。
import.*;
publicclassExercise12{
publicstaticvoidmain(String[]args){
String[]str={"Java","is","an","object","oriented","programming","language"};
ength();
}
avr=(double)length/;
"平均数的大小为:
"+avr);
}
}
14.定义一个4行4列的double型二维数组,并创建一个方法显示数组,计算任意给定的行,给定的列,主对角线和副对角线的和以及数组中的最大值。
import.*;
publicclassExercise14{
publicstaticvoidmain(String[]args)throwsIOException{
double[][]array={{2,4,5,7},
{6,3,5,7},
{6,7,4,4},
{2,2,3,3}};
ntValue();
"请输入选择的列(0-3):
");
InputStreamReaderinput2=newInputStreamReader;
BufferedReaderreader2=newBufferedReader(input2);
Stringline=();
intiline=newInteger(line).intValue();
doublesum=0;
for(inti=0;i<4;i++){
sum=sum+array[irow][i];
}
for(intj=0;j<4;j++){
sum=sum+array[j][iline];
}
;
publicclassExercise15{
publicstaticvoidmain(String[]args)throwsIOException{
"请输入字符串:
");
InputStreamReaderisStream=newInputStreamReader;
BufferedReaderbfReader=newBufferedReader(isStream);
Stringinput=();
char[]change=();
for(inti=0;i<();i++){
if(change[i]))
change[i]=(
change[i]);else
change[i]=(
change[i]);
}
"转化后的字符串:
");
for(inti=0;i<();
i++){
}
}
16.用一段代码检测两个double型的数x和y是否相等。
代码应能分辨这两个数是否是无穷大或NaN.如果它们相等,代码能正确显示这个数。
publicclassExercise16{
publicstaticvoidmain(String[]args){
doublex=;
doubley=0;
if(x==||y=={
"x或y无穷大!
");
}
if(x)||(y)){
"x或y无穷大!
");
}
写一个方法,把命令行输入的字符串转化为相应的int值。
思考:
如果字符串不能代表整数时该怎么办
import.*;
publicclassExercise17{
publicstaticvoidmain(String[]args)throwsNumberFormatException{
int[]array=newint[];
for(inti=0;i<;i++){
try{
newInteger(args[i]).intValue();
}
catch(Exceptione){
continue;
}
array[i]=newInteger(args[i]).intValue();
}
for(inti=0;i<;i++){
"*");
}
}}
19.创建一个有两个方法的程序。
标准的main()方法初始化两个变量,一个是String型,另一个是StringBuffer型,它们将作为第二个方法的输入参数,这个方法将把一个字符串连接在两个变量后面。
publicclassExercise19{
publicstaticStringconnect(String
str,StringBufferstrBuf){
Stringcon=str+strBuf+"123";
returncon;
}
publicstaticvoidmain(String[]args){
Stringstr="thisisaString!
";
StringBufferstrBuf=newStringBuffer("thisisaStringBuffer!
");
Stringcon=connect(str,strBuf);
"连接后的字符串为:
"+con);
}
}
20.创建一个简单的成绩单程序,帮助老师评估学生的表现。
该程序用double数组存放成绩来计算平均成绩。
;
publicclassExercise20{
publicstaticvoidmain(String[]args)throwsIOException{
inti=0;
double[]score=newdouble[20];
Stringstr="";
do{
"请输入第"+i+"个学生的成绩:
");
InputStreamReaderinput=newInputStreamReader;
BufferedReaderreader=newBufferedReader(input);
str=();
try{
doublescoreTemp=newDouble(str).doubleValue();
score[i]=scoreTemp;
i++;
}
catch(Exceptione)
{continue;}
}while(!
("#"));
for(intj=0;j
"//"
);}
doublescoreSum=0;
for(intj=0;j
scoreSum=scoreSum+score[j];
}
"输入的"+i+"个同学平均成绩是:
"+scoreSum/i);
}
}