admin 管理员组文章数量: 887021
2023年12月19日发(作者:mirtazapine)
04747 Java语言程序设计(一)2017年版
课后思考题与练习题
程序设计题参考答案
第三章 流程控制语句
5.1题
_____________________________________________________________________________
public class Chapt3_5_1 {
public static void main(String[] args) {
int n[ ]={2,4,6,8,10}; //分别计算2、4、6、8、10
int stepmulti=1;
for (int i=0;i<;i++){
stepmulti*=n[i];
n("n["+i+"]!="+n[i]+"!="+stepmulti);}
}
}
5.2题
_____________________________________________________________________________
public class Chapt3_5_2 {
public static void main(String[] args) {
float n=30.0f; //可以手动修改n值
float sum=0.000f;
for (float i=1;i<=n;i++)sum=sum+(1/i);
n("S=1+1/2+1/3+.....+1/n="+sum);
}
}
5.3题
_____________________________________________________________________________
public class Chapt3_5_3 {
public static void main(String[] args) {
float n=30.0f; //可以手动修改n值
float sum=1.000f;
for (float i=1; i<=n;i++)sum=sum+1/(3*i);
n("S=1+1/3+1/6+...+1/3n="+sum);
}
}
5.4题
_____________________________________________________________________________
public class Chapt3_5_4 {
public static void main(String[] args) {
int n=7;
int i=0;
while(i if (i for (int j=0; j for (int k=0; k }else { for (int m=0; m for (int k=0;k } i++; n(""); } } } 5.5题 _____________________________________________________________________________ public class Chapt3_5_5 { public static void main(String[] args) { int n=10; int i=0; while (i<10){ for (int j=1;j i++; n(); } } } 5.6题 _____________________________________________________________________________ public class Chapt3_5_6 { public static void main(String[] args) { for (int i=1;i<10;i++)("t "+i); for (int i=1;i<10;i++){ ("n"+i+"t"); for (int j=1;j<10;j++) (j+"*"+i+"="+i*j+"t"); } } } 5.7题 _____________________________________________________________________________ import r; public class Chapt3_5_7 { public static void main(String[] args) { String str; Scanner scan=new Scanner(); n("请输入一串字符:"); str=ne(); int k=0; goon: for(int i=0;i<();i++){ if (!(ing(i,i+1).equals(ing(()-i-1,()-i)))) { n("你输入的字符串("+str+")不是回文"); k=1; break; }else continue goon; } if (k==0) n("你输入的字符串("+str+")是回文"); (); } } 5.8题 _____________________________________________________________________________ import r; public class Chapt3_5_8 { public static void main(String[] args) { int n=0; Scanner scan=new Scanner(); n("请输入一个整数:"); int num=t(); for (int i=2;i if (num%i==0){n(num+"非质数!");break;} else n=1; } if (n==1)n(num+"是个质数!"); (); } } 5.9题 _____________________________________________________________________________ import r; public class Chapt3_5_9 { public static void main(String[] args) { Scanner t=new Scanner(); n("请输入华氏温度:"); double tin=uble(); n("转换后的摄氏温度为:"+(tin-32)/1.8); (); } } 5.10题 _____________________________________________________________________________ import r; public class Chapt3_5_10 { public static void main(String[] args) { double s; Scanner leng=new Scanner(); n("请输入三角形的第一条边长a:"); double a=uble(); n("请输入三角形的第二条边长b:"); double b=uble(); n("请输入三角形的第三条边长c:"); double c=uble(); if (a+b<=c||a+c<=b||b+c<=a||(a-b)>=c||(a-c)>=b||(b-c)>=a) ("您输入的三条边无法构成三角形"); //题目未要求判断,可以忽略 else { s=(a+b+c)/2; n("这个三角形的面积S为"+(s*(s-a)*(s-b)*(s-c))); } (); } } 5.11题 _____________________________________________________________________________ import ar; //获取系统当年年份需要用到 import r; /** * 根据本书进度编写的程序,重复代码比较多,读者可在学习后续章节后再优化 * */ public class Chapt3_5_11 { public static void main(String[] args) { Scanner in=new Scanner(); n("请输入数字1、2、3选择你要执行的操作,1、输入一个月份,打印当年该月的月历;2、输入当年的一个日期,查询当天是星期几;3、查询两个日期之间相差几天。"); int num1=t(); switch (num1) { case 1: //打印月历 int y0,mon02,w0; int days0=0; int mon=0; n("请输入你要打印月历的月份1~12"); int num2=t(); } Calendar cal0=tance(); //获取系统当年年份 y0=(); if (y0%400==0||y0%4==0) mon02=29; //判断闰年,确定2月份天数 else mon02=28; switch (num2){ //计算日是该年的第几天 case 1: days0=1;mon=31;break; case 2: days0=31+1;mon=mon02;break; case 3: days0=31+mon02+1;mon=31;break; case 4: days0=31+mon02+31+1;mon=30;break; case 5: days0=31+mon02+31+30+1;mon=31;break; case 6: days0=31+mon02+31+30+31+1;mon=30;break; case 7: days0=31+mon02+31+30+31+30+1;mon=31;break; case 8: days0=31+mon02+31+30+31+30+31+1;mon=31;break; case 9: days0=31+mon02+31+30+31+30+31+31+1;mon=30;break; case 10: days0=31+mon02+31+30+31+30+31+31+30+1;mon=31;break; case 11: days0=31+mon02+31+30+31+30+31+31+30+31+1;mon=30;break; case 12: days0=31+mon02+31+30+31+30+31+31+30+31+30+1;mon=31;break; w0=((y0-1)+(y0-1)/4-(y0-1)/100+(y0-1)/400+days0)%7; //计算当月首日 是星期几 n("日t"+"一t"+"二t"+"三t"+"四t"+"五t"+"六"); //打印表头 n(); for (int j=0;j ("t"); } int k=w0; for (int i=1;i<=mon;i++){ (i+"t"); k++; if (k>6){ n(); k=0; } } break; //满一周换行打印 case 2: //计算星期几 int y,m,d,w,month2; int days=0; String week=null;n("请输入你要查询的日期,格式为MMDD:"); int num3=t(); Calendar cal=tance(); //获取系统当年年份 y=(); m=num3/100; //提取月份 d=num3%100; //提取日期 if (y%400==0||y%4==0) month2=29; //判断闰年,确定2月份天数 else month2=28; switch (m){ //计算日是该年的第几天 case 1: days=d;break; case 2: days=d+31;break; case 3: days=31+month2+d;break; case 4: days=31+month2+31+d;break; case 5: days=31+month2+31+30+d;break; case 6: days=31+month2+31+30+31+d;break; case 7: days=31+month2+31+30+31+30+d;break; case 8: days=31+month2+31+30+31+30+31+d;break; case 9: days=31+month2+31+30+31+30+31+31+d;break; case 10: days=31+month2+31+30+31+30+31+31+30+d;break; case 11: days=31+month2+31+30+31+30+31+31+30+31+d;break; case 12: days=31+month2+31+30+31+30+31+31+30+31+30+d;break; } w=((y-1)+(y-1)/4-(y-1)/100+(y-1)/400+days)%7; //星期几的计算公式 switch (w){ //只是用于显示把0~6值转换为 中文的星期几 case 1: week="一";break; case 2: week="二";break; case 3: week="三";break; case 4: week="四";break; case 5: week="五";break; case 6: week="六";break; case 0: week="日";break; } n(y+"年"+m+"月"+d+"日是星期"+week); break; case 3: //计算日期相差天数 int y1,m1,d1,mon12,y2,m2,d2,mon22,daysinyear; int countdays=0; int days1=0; int days2=0; n("请输入起始日期,格式为YYYYMMDD:"); int num4=t(); n("请输入结束日期,格式为YYYYMMDD:"); int num5=t(); y1=num4/10000; //提取起始日年份 m1=(num4%10000)/100; //提取起始日月份 d1=num4%100; //提取起始日日期 y2=num5/10000; //提取结束日年份 m2=(num5%10000)/100; //提取结束日月份 d2=num5%100; //提取结束日日期 if (y1%400==0||y1%4==0) mon12=29; //判断起始日是否闰年,确定2月份else mon12=28; if (y2%400==0||y2%4==0) mon22=29; //判断结束日是否闰年,确定2月份 天数 天数 else mon22=28; switch (m1){ //计算起始日是该年的第几天 case 1: days1=d1;break; case 2: days1=31+d1;break; case 3: days1=31+mon12+d1;break; case 4: days1=31+mon12+31+d1;break; case 5: days1=31+mon12+31+30+d1;break; case 6: days1=31+mon12+31+30+31+d1;break; case 7: days1=31+mon12+31+30+31+30+d1;break; case 8: days1=31+mon12+31+30+31+30+31+d1;break; case 9: days1=31+mon12+31+30+31+30+31+31+d1;break; case 10: days1=31+mon12+31+30+31+30+31+31+30+d1;break; 天数 } } case 11: days1=31+mon12+31+30+31+30+31+31+30+31+d1;break; case 12: days1=31+mon12+31+30+31+30+31+31+30+31+30+d1;break; switch (m2){ //计算结束日是该年的第几天 case 1: days2=d2;break; case 2: days2=31+d2;break; case 3: days2=31+mon22+d2;break; case 4: days2=31+mon22+31+d2;break; case 5: days2=31+mon22+31+30+d2;break; case 6: days2=31+mon22+31+30+31+d2;break; case 7: days2=31+mon22+31+30+31+30+d2;break; case 8: days2=31+mon22+31+30+31+30+31+d2;break; case 9: days2=31+mon22+31+30+31+30+31+31+d2;break; case 10: days2=31+mon22+31+30+31+30+31+31+30+d2;break; case 11: days2=31+mon22+31+30+31+30+31+31+30+31+d2;break; case 12: days2=31+mon22+31+30+31+30+31+31+30+31+30+d2;break; for (int yy=y1;yy if (yy%400==0||yy%4==0) daysinyear=366; else daysinyear=365; countdays+=daysinyear; } countdays=countdays+days2-days1; //判断闰年,确定一年的总 //累加各年的天数 n(y1+"年"+m1+"月"+d1+"日与"+y2+"年"+m2+"月"+d2+"相差"+countdays+"天"); break; } (); } } 5.12题 _____________________________________________________________________________ import r; public class Chapt3_5_12 { public static void main(String[] args) { String unit=null; int j,m; int k=20; Scanner input=new Scanner(); n("请输入金额:"); Float in=oat(); j=(int)(in*100); n(in+"元="); for (int i=10000;i>=1;i=i*10/k){ switch (i){ //确定币值和单位 case 10000: case 5000: case 2000: case 1000: case 500: case 200: case 100: unit="元";m=i/100;break; case 50: case 20: case 10: unit="角";m=i/10;break; default: unit="分";m=i; } if (j/i>0) n(m+unit+"t"+j/i+"个"); j=j-i*(j/i); if (i==5000||i==500||i==50) k=25; else k=20; } (); } } 5.13题 _____________________________________________________________________________ import r; import r; public class Chapt3_5_13 { public static void main(String[] args) { Scanner numinput=new Scanner(); n("请输入一个正整数:"); int num1=t(); //注意int型最大表示范围 Chapt3_5_13 a=new Chapt3_5_13(); (num1); (); } public void revInt(int a){ n("您输入的正整数:"+a); String str=f(a); int b=(); /** * 如果末位或后几位均为0,按Int型重新排列后会丢失这些0,按字符串重新排列,末位为0的情况不丢失。 */ } } String out; ("按数字顺序相反排列后为:"); for (int i=b;i>0;i--){ out=ing(i-1,i); (out); } 5.14题 _____________________________________________________________________________ public class Chapt3_5_14 { public static void main(String[] args) { int num=0,max=0,count=0; int min=100; } } } n("100个随机数为:"); for (int i=0;i<100;i++) { num=(int)(()*100); n("num"+i+"="+num); if (num>50) count++; if (num>max) max=num; if (num n("最大值为:"+max+"t最小值为:"+min); n(">50的数共有:"+count+"个");
版权声明:本文标题:04747Java语言程序设计教材程序设计题答案 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1702938107h436659.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论