Java上机程序复习题

上传人:xt****7 文档编号:134741881 上传时间:2022-08-13 格式:DOC 页数:27 大小:122.51KB
收藏 版权申诉 举报 下载
Java上机程序复习题_第1页
第1页 / 共27页
Java上机程序复习题_第2页
第2页 / 共27页
Java上机程序复习题_第3页
第3页 / 共27页
资源描述:

《Java上机程序复习题》由会员分享,可在线阅读,更多相关《Java上机程序复习题(27页珍藏版)》请在装配图网上搜索。

1、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(循环变量=初始

2、值;循环变量=终止值;循环变量=循环变量*步长值)循环体语句;while循环语句:当关系表达式成立时,执行循环体中的语句,然后返回重新检验关系表达式是否成立,若不成立则不执行循环体中语句,结束循环。-while(关系表达式)循环体语句块;-do.while循环语句:首先执行循环体中语句块,然后检验关系表达式是否成立,若成立,则继续执行循环体中语句,否则,结束循环。-do循环体语句块;while(关系表达式)数组的声明方法:数据类型 数组名称 =new 数据类型元素个数;数据类型 数组名称 =数据集合;例如:double s=new double20;double t=12,34,56,78,9

3、8;数组的输入模块:int i;for(i=0;i20;i=i+1)ai=Math.floor(Math.random()*(N-M+1)+M;for(i=0;i5;i=i+1)s1=input1.readLine();ai=Double.parseDouble(s1);数组的输出模块:for(i=0;i20;i=i+1)System.out.println(ai);随机函数的使用:随机函数:Math.random() 返回0,1.0) 之间的随机数。0=Math.random()1生面某范围内随机整数:1 产生01这间的随机小数x x=Math.random( );2 产生0,n这间的随机整

4、数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.*; 键盘

5、输入时打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.pr

6、int(请输入身高(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)/2public class a3 public static void main(String args) double a,b,c,p,s;a=

7、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)import java.io.*;public class a5 public static void main(String args)throws IOException InputStreamReader read1=new InputStreamReader(System.in)

8、; BufferedReader input1=new BufferedReader(read1); String s1; double x,y; System.out.print(请输入x:); s1=input1.readLine(); x=Double.parseDouble(s1); if (x2) 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(St

9、ring 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);

10、 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

11、=input1.readLine(); x=Double.parseDouble(s1); if (x50) 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 e04public static void main(String args) throws IOExceptionInputStreamReader reader=new InputStreamReader(System.in);

12、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

13、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 (x0) System.out.println(x为正数); else System.out.println(

14、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

15、.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 (ab) if (ac) System.out.println(最大数是+a); else System.out.println(最大数是+c); else if (bc) System.out.println(最大数是+b); else System.out.prin

16、tln(最大数是+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:);s

17、1=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 (d0) System.out.println(无解); else if (d=0) x1=(-b+Math.sqrt(d)/(2*a);System.out.println(x=+x1);

18、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 p

19、ublic 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 a1

20、8 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求之和。165public 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求之和。132621求之和。11022求提示:算式中每个分数的分母有规律,则用循环变量来描述分母的变化过程,但是每次累加的内容是分母所对应的整个分

21、数。(如:若分数的结构是分子为1、分母为x,则分数为1/x)23求之和。17.354641295237272public 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求之和。44200public class a24 public static void main(String args) int x,s=0;for (x=1;x=50;x=x+

22、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.7158912E927求之和。1111111public class a27 public static void main(String args) double x,s=0;for (x=0;x=6;x=x+1

23、) s=s+Math.pow(10,x); System.out.println(s); 28求之和。1234567public 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.9990234375public class a29 public static void main(String args) double x,s=0;for (x=2;x=1024;x

24、=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求之和。22032求之和。42925public class a32 public static void main(String args) int x,s=0,n=0;for (x=1;x=99;x=x

25、+2) s=s+x;n=n+s; System.out.println(n); 33有一群龟鹤,头150只,足400只,问龟鹤各有几只?50,100public 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,721public class a34 public stat

26、ic 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最小的值。20public 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);

27、37当时,求k最大的值。26public class a37 public static void main(String args) int x=0,s=0;while (s200) x=x+2;s=s+x; System.out.println(x-2); 38二分硬币和五分硬币共40枚,1.31元,问每种硬币各有多少枚?二分硬币:23,五分硬币17public 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.prin

28、tln(2分:+t+ 5分:+f); 39下式中两个囗号内是一个相同的数,它到底是多少? 囗36528=3囗8256数字是:4public 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,25public class a40 public static void main(String args) int x,y;for (

29、x=1;x=20;x=x+1) y=40-x;if (x*y=375) System.out.println(x+ +y); 41求出所有三位正整数的各位数码之和。和:12600public 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

30、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=111+555+333,所以153是水仙花数。求所有的水仙花数。public class a43 public stat

31、ic 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,40744有一个两位正整数,加6后再把其个位数字与十位数字互换得到一个新的两位数,这样加6再互换共三次后,又得到了原来的两位数。求这样的两位数都有哪些?共有5个:19,41,52, 74,85public class a44 public static vo

32、id main(String args) int x,a,b,n,y;for (x=10;x=99;x=x+1) n=x;for (y=1;y=100) break; if (n=x) System.out.println(x); 45显示出所有的三位质数。共143个提示:判断x为质数,除了1和x本身之外,不能整除其它整数,则说明x是质数。换句话说:如果x除了能整除1和x本身之外,还能整除2(x-1)之间的某个整数,则说明x不是质数,而是合数。public class a45 public static void main(String args) int x,y,f=0,s=0;for (x

33、=100;x=999;x=x+1) f=0;for (y=2;y=x-1;y=y+1) if (x%y=0) f=1; if (f=0) System.out.println(x);s=s+1; System.out.println(s); 46求所有两位质数的和。 1043public class a46 public static void main(String args) int x,y,f=0,s=0;for (x=10;x=99;x=x+1) f=0;for (y=2;y=3)。显示出该组数列中的前20项的数值。public class a47 public static void

34、 main(String args) int a=new int20;int x;a0=1;a1=1;for (x=2;x=19;x=x+1) ax=ax-1+ax-2; for (x=0;x=19;x=x+1)System.out.println(ax); 48我国古代百马问题:百马驮百瓦,大马驮块,老马驮块,两匹小马驮块。问大马、老马、小马各有多少匹?2 30 685 25 708 20 7211 15 7414 10 7617 5 78public class a48 public static void main(String args) int x,y,z;for (x=2;x=10

35、0;x=x+2) for (y=1;y=0) System.out.println(老马:+z+ 大马:+y+ 小马:+x); 49古代数学问题:鸡、兔、九头鸟同笼,头百只、脚百只。问鸡、兔、九头鸟各有多少只?14 14 831 6 7public class a49 public static void main(String args) int x,y,z;for (x=9;x=100;x=x+9) for (y=1;y0) System.out.println(鸡:+z+ 兔:+y+ 九头鸟:+x); 50蜘蛛有条腿,蜻蜓有条腿对翅膀,蝉有条腿对翅膀。三种昆虫共有只,条腿和对翅膀,问每种

36、昆虫各有多少只?蜘蛛5 蜻蜓7 蝉6public class a50 public static void main(String args) int x,y,z;for (x=1;x=18;x=x+1) for (y=1;y=18;y=y+1) z=18-x-y;if (8*x+6*y+6*z=118&2*y+z=20) System.out.println(蜘蛛:+x+ 蜻蜓:+y+ 蝉:+z); 51将26个英文大写字母存储于数组c中,显示数组c中的所有数据。public class a51 public static void main(String args) int a=new i

37、nt26;char c=new char26;int x;for (x=0;x=25;x=x+1) ax=x+65;cx=(char)ax;System.out.println(cx); 52.将1,100的所有整数存储于数组d中,并显示数组d中的所有数据。 public class a52 public static void main(String args) int d=new int100;int x;for (x=0;x=99;x=x+1) dx=x+1;System.out.println(dx); 54随机产生100个10,20整数,将这些数存储于数组a中,并显示数组中的所有数据

38、。public class a54 public static void main(String args) int a=new int100;int x;for (x=0;x=99;x=x+1) ax=(int)Math.floor(20-10+1)*Math.random( )+10);System.out.println(ax); 55随机产生50个100,150整数,将这些存储于数组a中,并显示数组中的所有数据。public class a55 public static void main(String args) int a=new int50;int x;for (x=0;x=4

39、9;x=x+1) ax=(int)Math.floor(150-100+1)*Math.random( )+100);System.out.println(ax); 57键盘输入10个正整数(1000),将它们存储于数组a中,并显示数组a中所有的数据。import java.io.*;public class a57 public static void main(String args)throws IOException InputStreamReader read1=new InputStreamReader(System.in); BufferedReader input1=new B

40、ufferedReader(read1); String s1; int a=new int10; int x; for (x=0;x=1000) System.out.print(请重新输入:);s1=input1.readLine(); ax=Integer.parseInt(s1); for (x=0;x=9;x=x+1) System.out.println(ax); 58键盘输入5个数,将它们存储于数组a中,并显示数组a中所有的数据。59有一组数,其第一个元素数值是1,以后每项的内容都是其前一项的3倍加1,显示这组数的前20项内容。public class a59 public static void main(String args) double a=new double20;a0=1;int x;for (x=1;x=19;x=x+1) ax=ax-1*3+1; for (x=0;x=19;x=x+1) System.out.println(ax); 60随机产生10个20,30的整数,将它们存储于数组a中,并显示数组a中的所有数据。然后求数组a中所有数据的和。public class a60 public static void main(String args) int a=new int10;int x

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!