收藏 分销(赏)

Java上机程序复习题.doc

上传人:xrp****65 文档编号:6110289 上传时间:2024-11-28 格式:DOC 页数:27 大小:134.50KB 下载积分:10 金币
下载 相关 举报
Java上机程序复习题.doc_第1页
第1页 / 共27页
Java上机程序复习题.doc_第2页
第2页 / 共27页


点击查看更多>>
资源描述
Java的基本数据类型: int 整数类型 long 长整数类型 float 单精度浮点数类型 double 双精度浮点数类型 char 字符类型 打印语句: System.out.print(s); System.out.print(“s=”+s); System.out.println(a); System.out.println(“a=”+a); 条件语句: 格式1: if(条件关系式) { 语句块1; } 格式2: if(条件关系式) { 语句块1; } else { 语句块2; } 关系运算符: 等于==不等于!= 逻辑运算符: 非 !与 &&或 || for循环语句: for(循环变量=初始值;循环变量<=终止值;循环变量=循环变量+步长值) { 循环体语句; } for(循环变量=初始值;循环变量>=终止值;循环变量=循环变量-步长值) { 循环体语句; } for(循环变量=初始值;循环变量<=终止值;循环变量=循环变量*步长值) { 循环体语句; } while循环语句:→ 当关系表达式成立时,执行循环体中的语句,然后返回重新检验关系表达式是否成立,若不成立则不执行循环体中语句,结束循环。 -------------------------------------------------------- while(关系表达式) { 循环体语句块; } --------------------------------------------- do...while循环语句:→ 首先执行循环体中语句块,然后检验关系表达式是否成立,若成立,则继续执行循环体中语句,否则,结束循环。 -------------------------------------------------------- do { 循环体语句块; }while(关系表达式) 数组的声明方法: 数据类型 数组名称[ ]=new 数据类型[元素个数]; 数据类型 数组名称[ ]={数据集合}; 例如: double s[]=new double[20]; double t[]={12,34,56,78,98}; 数组的输入模块: int i; for(i=0;i<20;i=i+1) { a[i]=Math.floor(Math.random()*(N-M+1))+M; } for(i=0;i<5;i=i+1) { s1=input1.readLine(); a[i]=Double.parseDouble(s1); } 数组的输出模块: for(i=0;i<20;i=i+1) { System.out.println(a[i]); } 随机函数的使用: 随机函数:Math.random() 返回[0,1.0) 之间的随机数。0<=Math.random()<1 生面某范围内随机整数: 1. 产生0~1这间的随机小数x x=Math.random( ); 2. 产生[0,n]这间的随机整数x x=(int)Math.floor((n+1)*Math.random( )); 3. 产生[m,n]范围内的随机整数x x=(int)Math.floor((n-m+1)*Math.random( )+m); 练习: 1. 已知一般人平均每磅体重每天需19卡路里,若已知某人的体重(单位:千克),求此人一天需要多少卡路里?(1磅约为0.455千克) 2. 身体质量指数(BMI)是衡量身体健康与否的一种标准,一般认为身体质量指数(BMI)在20至25之间是健康的。计算BMI的公式:体重除以身高的平方(体重单位为千克,身高单位为米)。根据自己的实际情况,计算出自己的BMI。 import java.io.*; ←键盘输入时打 public class a2 { public static void main(String args[])throws IOException { InputStreamReader read1=new InputStreamReader(System.in); BufferedReader input1=new BufferedReader(read1); String s1; double g,h,bmi; System.out.print("请输入体重(kg):"); s1=input1.readLine(); g=Double.parseDouble(s1); System.out.print("请输入身高(m):"); s1=input1.readLine(); h=Double.parseDouble(s1); bmi=g/(h*h); System.out.println("BMI:"+bmi); } } 3.已知三角形边长分别为33、35、12,利用海伦公式求其面积。 海伦公式:(其中,平方根的表示法:Math.sqrt(x)) 假设有一个三角形,边长分别为a、b、c,三角形的面积S可由以下公式求得: S=    而公式里的p为半周长:p=(a+b+c)/2 public class a3 { public static void main(String args[]) { double a,b,c,p,s; a=33;b=35;c=12; p=(a+b+c)/2; s=Math.sqrt(p*(p-a)*(p-b)*(p-c)); System.out.println("面积:"+s); } } 4.已知一个圆的半径是20cm,求该圆的周长与面积。其中,圆周率的表示法:Math.PI。 5.当给出X的值时,求下列函数的值: y=0 (x<0) y=x的平方根 (x>=0) import java.io.*; public class a5 { public static void main(String args[])throws IOException { InputStreamReader read1=new InputStreamReader(System.in); BufferedReader input1=new BufferedReader(read1); String s1; double x,y; System.out.print("请输入x:"); s1=input1.readLine(); x=Double.parseDouble(s1); if (x<0) { y=0; } else { y=Math.sqrt(x); } System.out.println("y="+y); } } 6. 某商场对苹果进行促销,规定购买2公斤以上可以在原价每公斤1.5元的基础上打8折,设计一个程序计算购买X公斤苹果的应付款。 import java.io.*; public class a6 { public static void main(String args[])throws IOException { InputStreamReader read1=new InputStreamReader(System.in); BufferedReader input1=new BufferedReader(read1); String s1; double x,y; System.out.print("请输入x:"); s1=input1.readLine(); x=Double.parseDouble(s1); if (x>2) { y=x*1.5*0.8; } else { y=x*1.5; } System.out.println("y="+y); } } 7. 如果一个数能被7整除,则输出这个数,否则输出“此数不能整除7” import java.io.*; public class a7 { public static void main(String args[])throws IOException { InputStreamReader read1=new InputStreamReader(System.in); BufferedReader input1=new BufferedReader(read1); String s1; double x; System.out.print("请输入x:"); s1=input1.readLine(); x=Double.parseDouble(s1); if (x%7==0) { System.out.println("x="+x); } else { System.out.println("此数不能整除7"); } } } 8. 火车行李托运费,行李重量在50kg以下,每千克按0.10元计,如50kg,超出部分每千克按0.20元计。 import java.io.*; public class a8 { public static void main(String args[])throws IOException { InputStreamReader read1=new InputStreamReader(System.in); BufferedReader input1=new BufferedReader(read1); String s1; double x,y; System.out.print("请输入x:"); s1=input1.readLine(); x=Double.parseDouble(s1); if (x<50) { y=x*0.1; } else { y=x*0.1+(x-50)*0.2; } System.out.println("价格:"+y); } } 9. 闰年判断:判断条件是:该年份能被4但不能被100整除,或能被400整除。 import java.io.*; public class e04 { public static void main(String args[]) throws IOException { InputStreamReader reader=new InputStreamReader(System.in); BufferedReader input=new BufferedReader(reader); System.out.print("Enter the 年份:"); String s1=input.readLine(); int x=Integer.parseInt(s1); if ((x%4==0)&(x%100!=0)||(x%400==0)) System.out.println(x+"是闰年"); else System.out.println(x+"不是闰年"); } } 10. 通过键盘输入一个数,判断一个数是正数、零还是负数。 import java.io.*; public class a10 { public static void main(String args[])throws IOException { InputStreamReader read1=new InputStreamReader(System.in); BufferedReader input1=new BufferedReader(read1); String s1; double x; System.out.print("请输入x:"); s1=input1.readLine(); x=Double.parseDouble(s1); if (x<0) { System.out.println("x为负数"); } else { if (x>0) { System.out.println("x为正数"); } else { System.out.println("x为0"); } } } } 11. 输入三个整数,输出其中最大数。 import java.io.*; public class a11 { public static void main(String args[])throws IOException { InputStreamReader read1=new InputStreamReader(System.in); BufferedReader input1=new BufferedReader(read1); String s1; double a,b,c; System.out.print("请输入a:"); s1=input1.readLine(); a=Double.parseDouble(s1); System.out.print("请输入b:"); s1=input1.readLine(); b=Double.parseDouble(s1); System.out.print("请输入c:"); s1=input1.readLine(); c=Double.parseDouble(s1); if (a>b) { if (a>c) { System.out.println("最大数是"+a); } else { System.out.println("最大数是"+c); } } else { if (b>c) { System.out.println("最大数是"+b); } else { System.out.println("最大数是"+c); } } } } 12. 求解二次方程Ax2+Bx+C=0的根,系数A,B,C由键盘输入 import java.io.*; public class a12 { public static void main(String args[])throws IOException { InputStreamReader read1=new InputStreamReader(System.in); BufferedReader input1=new BufferedReader(read1); String s1; double a,b,c,d,x1,x2; System.out.print("请输入a:"); s1=input1.readLine(); a=Double.parseDouble(s1); System.out.print("请输入b:"); s1=input1.readLine(); b=Double.parseDouble(s1); System.out.print("请输入c:"); s1=input1.readLine(); c=Double.parseDouble(s1); d=b*b-4*a*c; if (d<0) { System.out.println("无解"); } else { if (d==0) { x1=(-b+Math.sqrt(d))/(2*a); System.out.println("x="+x1); } else { x1=(-b+Math.sqrt(d))/(2*a); x2=(-b-Math.sqrt(d))/(2*a); System.out.println("x1="+x1); System.out.println("x2="+x2); } } } } 13. 显示所有100内的偶数; public class a13 { public static void main(String args[]) { int x; for (x=2;x<=100;x=x+2) { System.out.println(x); } } } 14.显示所有100内的奇数; 15.显示所有200-300间的偶数; 16.显示所有100内有能被7整除的数 public class a16 { public static void main(String args[]) { int x; for (x=1;x<=100;x=x+1) { if (x%7==0) { System.out.println(x); } } } } 17.显示所有既能被3整除又能被7整除的两位正整数,数值之间用“:”隔开。 public class a17 { public static void main(String args[]) { int x; for (x=10;x<=99;x=x+1) { if (x%7==0&x%3==0) { System.out.print(x+":"); } } } } 18.显示所有能被3整除的两位正奇数,数值之间用“-”隔开。 public class a18 { public static void main(String args[]) { int x; for (x=11;x<=99;x=x+2) { if (x%3==0) { System.out.print(x+"-"); } } } } 19.求之和。165 public class a19 { public static void main(String args[]) { int x,s=0; for (x=3;x<=30;x=x+3) { s=s+x; } System.out.println(s); } } 20.求之和。1326 21.求之和。110 22.求之和。4.18737751763962 提示:算式中每个分数的分母有规律,则用循环变量来描述分母的变化过程,但是每次累加的内容是分母所对应的整个分数。 (如:若分数的结构是分子为1、分母为x,则分数为1/x) 23.求之和。17.354641295237272 public class a23 { public static void main(String args[]) { double x,s=0; for (x=2;x<=21;x=x+1) { s=s+(x-1)/x; } System.out.println(s); } } 提示:若分子为x,则分母为x+1,则分数为:x/(x+1) 24.求之和。44200 public class a24 { public static void main(String args[]) { int x,s=0; for (x=1;x<=50;x=x+1) { s=s+x*(x+1); } System.out.println(s); } } 25.求之积。3628800 提示:注意累乘变量的初始值是什么值? public class a25 { public static void main(String args[]) { int x,s=1; for (x=1;x<=10;x=x+1) { s=s*x; } System.out.println(s); } } 26.求之积。3.7158912E9 27.求之和。1111111 public class a27 { public static void main(String args[]) { double x,s=0; for (x=0;x<=6;x=x+1) { s=s+Math.pow(10,x); } System.out.println(s); } } 28.求之和。1234567 public class a28 { public static void main(String args[]) { double x,s=0,n=0; for (x=0;x<=6;x=x+1) { s=s+Math.pow(10,x); n=n+s; } System.out.println(n); } } 29.求之和。0.9990234375 public class a29 { public static void main(String args[]) { double x,s=0; for (x=2;x<=1024;x=x*2) { s=s+1/x; } System.out.println(s); } } 30.求之和。4037913 提示:先求乘积的算式,再累加所求的积。 public class a30 { public static void main(String args[]) { int x,s=1,n=0; for (x=1;x<=10;x=x+1) { s=s*x; n=n+s; } System.out.println(n); } } 31.求之和。220 32.求之和。42925 public class a32 { public static void main(String args[]) { int x,s=0,n=0; for (x=1;x<=99;x=x+2) { s=s+x; n=n+s; } System.out.println(n); } } 33.有一群龟鹤,头150只,足400只,问龟鹤各有几只?50,100 public class a33 { public static void main(String args[]) { int g,h; for (g=1;g<=150;g=g+1) { h=150-g; if (g*4+h*2==400) { System.out.println("龟:"+g+" 鹤:"+h); } } } } 34.一个数被2、3、4、5、6除都余1,而正好能被7整除,问1000以内的自然数中这样的数都有哪些?301,721 public class a34 { public static void main(String args[]) { int x; for (x=1;x<=1000;x=x+1) { if (x%2==1&x%3==1&x%4==1&x%5==1&x%6==1&x%7==0) { System.out.println(x); } } } } 35.一个数被2除余1,被3除余2,被4除余3,被5除余4。在500以内的自然数中这样的数有哪几个? 36.当时,求k最小的值。20 public class a36 { public static void main(String args[]) { int x=0,s=0; while (s<=200) { x=x+1; s=s+x; } System.out.println(x-1); } } 37.当时,求k最大的值。26 public class a37 { public static void main(String args[]) { int x=0,s=0; while (s<200) { x=x+2; s=s+x; } System.out.println(x-2); } } 38.二分硬币和五分硬币共40枚,1.31元,问每种硬币各有多少枚? 二分硬币:23,五分硬币17 public class a38 { public static void main(String args[]) { int t,f; for (t=1;t<=40;t=t+1) { f=40-t; if (t*2+f*5==131) { System.out.println("2分:"+t+" 5分:"+f); } } } } 39.下式中两个囗号内是一个相同的数,它到底是多少? 囗3×6528=3囗×8256 数字是:4 public class a39 { public static void main(String args[]) { int x,f; for (x=0;x<=9;x=x+1) { if ((x*10+3)*6528==(30+x)*8256) { System.out.println(x); } } } } 40.两数之和是40,它们的积是375,求此二数。 这两个数是:15,25 public class a40 { public static void main(String args[]) { int x,y; for (x=1;x<=20;x=x+1) { y=40-x; if (x*y==375) { System.out.println(x+" "+y); } } } } 41.求出所有三位正整数的各位数码之和。 和:12600 public class a41 { public static void main(String args[]) { int x,a,b,c,s=0; for (x=100;x<=999;x=x+1) { a=(int)x/100; b=(int)x/10%10; c=(int)x%10; s=s+a+b+c; } System.out.println(s); } } 42.求出所有百位数字与十位数字之和等于个位数字的三位正整数。 共有45个。 public class a42 { public static void main(String args[]) { int x,a,b,c,s=0; for (x=100;x<=999;x=x+1) { a=(int)x/100; b=(int)x/10%10; c=(int)x%10; if (a+b==c) { System.out.println(x); s=s+1; } } System.out.println(s); } } 43.在自然数中,如果一个三位数等于其自身各个数字立方和,这样的三位数称为“水仙花数”。如:153=1×1×1+5×5×5+3×3×3,所以153是水仙花数。求所有的水仙花数。 public class a43 { public static void main(String args[]) { int x,a,b,c,s=0; for (x=100;x<=999;x=x+1) { a=(int)x/100; b=(int)x/10%10; c=(int)x%10; if (a*a*a+b*b*b+c*c*c==x) { System.out.println(x); } } } } 共有4个:153,370,371,407 44.有一个两位正整数,加6后再把其个位数字与十位数字互换得到一个新的两位数,这样加6再互换共三次后,又得到了原来的两位数。求这样的两位数都有哪些? 共有5个:19,41,52, 74,85. public class a44 { public static void main(String args[]) { int x,a,b,n,y; for (x=10;x<=99;x=x+1) { n=x; for (y=1;y<=3;y=y+1) { a=(int)n/10; b=(int)n%10; n=b*10+a; n=n+6; if (n>=100) { break; } } if (n==x) { System.out.println(x); } } } } 45.显示出所
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 行业资料 > 医学/心理学

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服