2022年java实验题.pdf
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《2022年java实验题.pdf》由会员分享,可在线阅读,更多相关《2022年java实验题.pdf(22页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、java 实验题实验一基本程序设计(1) 编写一个程序 , 读入一笔费用与酬金率 , 计算酬金与总钱数。 例如, 如果用户输入 10 作为费用 ,15%作为酬金率 , 计算结果显示酬金为¥ 1、5, 总费用为¥ 11、5。 public class Exercise2_5 public static void main(String args) / Read subtotal java、util、Scanner input = new java、util、Scanner(System、in); System、out 、print(Enter subtotal: ); double subtot
2、al = input、nextDouble(); / Read subtotal System、out 、print(Enter gratuity rate: ); double rate = input、nextDouble(); double gratuity = subtotal * rate / 100; double total = subtotal + gratuity; System、out 、println(Gratuity is + gratuity); System、out 、println(Total is + total); (2) (求 ASCII 码对应的字符 )编
3、写程序接受一个ASCII 码( 从 0 到 128 的整数 ),然后显示它所代表的字符。例如用户输入的就是97, 程序显示的就是俄字符a。 public class Exercise2_8 public static void main(String args) java、util、Scanner input = new java、util、Scanner(System、in); / Enter an ASCII code System、out 、print(Enter an ASCII code: ); int code = input、nextInt(); / Display result
4、 System、out 、println(The character for ASCII code + code + is + (char)code); (3) (计算一个三角形周长) 编写程序 , 读取三角形的三条边, 如果输入值合法就计算这个三角形的周长 ; 否则, 显示这些输入值不合法。 如果任意两条边的与大于第三边 , 那么输入值都就是合法的。public class Exercise3_25 public static void main(String args) java、util、Scanner input = new java、util、Scanner(System、in);
5、/ Enter three edges System、out 、print( Enter three edges (length in double): ); double edge1 = input、nextDouble(); double edge2 = input、nextDouble(); double edge3 = input、nextDouble(); 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 1 页,共 22 页 - - - - - - - - - - java 实验题 / Disp
6、lay results boolean valid = (edge1 + edge2 edge3) & (edge1 + edge3 edge2) & (edge2 + edge3 edge1); if (valid) System、out、println(The perimeter of the triangle is + (edge1 + edge2 + edge3); else System、out、println(Input is invalid); (4) (解一元二次方程 )求一元二次方程 ax2 + bx + c = 0 的两个根 ,b 就有*b-4ac称作一元二次方程的判别式。
7、如果它就是正值, 那么一元二次方程俄就有两个正根。如果它为0, 方程就只有一个根。如果它就是负值, 方程无实根。编写程序 ,提示用户输入 a、b 与 c 的值, 并且显示基于判别式的结果。如果判别式为正, 显示两个根 , 如果判别式为 0, 显示一个根 , 如果判别式为负 , 显示方程无实根。(4) import java、util、Scanner; public class Exercise3_1 public static void main(String args) Scanner input = new Scanner(System、in); System、out 、print(Ent
8、er a, b, c: ); double a = input、nextDouble(); double b = input、nextDouble(); double c = input、nextDouble(); double discriminant = b * b - 4 * a * c; if (discriminant 0) double r1 = (-b + Math、pow(discriminant, 0、5) / (2 * a); double r2 = (-b - Math、pow(discriminant, 0、5) / (2 * a); System、out 、print
9、ln(The roots are + r1 + and + r2); (5) ( 统计正数与负数的个数 , 然后计算这些数的平均值) 编写程序 , 读入未指定个数的整数 , 判断读入的正数有多少个, 读入的负数有多少个 , 然后计算这些输入值的总与及其平均值 (不对 0 计数) 。当输入为 0 时, 表示程序结束。将平均值以浮点数显示。(5)import java、util、Scanner; 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 2 页,共 22 页 - - - - - - - - - - ja
10、va 实验题 public class Exercise4_1 public static void main(String args) int countPositive=0, countNegative = 0; int count = 0, total = 0, num; Scanner input = new Scanner(System、in); System、out 、print( Enter an int value, the program exits if the input is 0: ); num = input、nextInt(); while (num != 0) i
11、f (num 0) countPositive+; else if (num 0) countNegative+; total += num; count+; / Read the next number num = input、nextInt(); if (count = 0) System、out、println(You didnt enter any number); else System、 out 、 println(The number of positives is + countPositive); System、 out 、 println(The number of neg
12、atives is + countNegative); System、out、println(The total is + total); System、out、println(The average is + total * 1、0 / count); 试验二方法(1) 一个五角数被定义为n(3n-1)/2,其中 n=1,2, 。所以 , 开始的几个数字就就是 1,5,12,22 , 编写下面的方法返回一个五角数: public static int getPentagonaNumber(int n) 编写一个测试程序显示前100 个五角数 , 每行显示 10 个。提示: 通过 for 循环
13、语句打印前 100 个五角数。(1)/ Exercise5_1、java: public class Exercise5_1 public static void main(String args) for (int i = 1; i side3) & (side1 + side3 side2) & (side2 + side3 side1); public static double area(double side1, double side2, double side3) double s = (side1 + side2 + side3) / 2; return Math、sqrt(s
14、 * (s - side1) * (s - side2) * (s - side3); 试验三数组(1) 编写程序 , 读取 10 个整数 , 然后按照与读入顺序相反的顺序将它们显示出来。提示:int num = new int10。(1) public class Exercise6_2 public static void main (String args) java、util、Scanner input = new java、util、Scanner(System、in); int num = new int10; for (int i = 0; i = 0; i-) System、o
15、ut、println(numi); (2)( 指定等级 ) 编写一个程序 , 读入学生成绩 , 获取最高分best, 然后根据下面的规则赋等级值 : 如果分数 = best 10, 等级为 A 如果分数 = best 20, 等级为 B 如果分数 = best 30, 等级为 C 如果分数 = best 40, 等级为 D 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 6 页,共 22 页 - - - - - - - - - - java 实验题其她情况下 , 等级为 F 程序提示用户输入学生总数, 然
16、后提示用户输入所有的分数, 最后显示等级得出结论。(2) import java、util、Scanner; public class Exercise6_1 /* Main method */ public static void main(String args) / Create a Scanner Scanner input = new Scanner(System、in); / Get number of students System、out 、print(Enter number of students: ); int numberOfStudents = input、nextI
17、nt(); int scores = new intnumberOfStudents; / Array scores int best = 0; / The best score char grade; / The grade / Read scores and find the best score System、out 、print(Enter + numberOfStudents + scores: ); for (int i = 0; i best) best = scoresi; / Declare and initialize output string String output
18、 = ; / Assign and display grades for (int i = 0; i = best - 10) grade = A; else if (scoresi = best - 20) grade = B; else if (scoresi = best - 30) grade = C; else if (scoresi = best - 40) grade = D; else grade = F; output += Student + i + score is + scoresi + and grade is + grade + n; / Display the r
19、esult System、out 、println(output); 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 7 页,共 22 页 - - - - - - - - - - java 实验题(3) (计算数字的出现次数 ) 编写程序 , 读取在 1 到 100之间的整数 , 然后计算每个数出现的次数。假定输入就是以0 结束的。(3)public class Exercise6_3 public static void main (String args) java、util、Scanner inpu
20、t = new java、util、Scanner(System、in); int counts = new int100; System、out 、print(Enter the integers between 1 and 100: ); / Read all numbers int number = input、nextInt(); / number read from a file while (number != 0) countsnumber - 1+; number = input、nextInt(); / Display result for (int i = 1; i 0)
21、System、out 、println(i + 1) + occurs + countsi + (countsi = 1) ? time : times); (4) 编写一个方法 , 使用下面的方法头求出一个整数数组中的最小元素: public static double min(double array) 编写测试程序 , 提示用户输入 10 个数字 , 调用这个方法 , 返回最小元素值。(4)public class Exercise6_9 / Main method public static void main(String args) double numbers = new dou
22、ble10; java、util、Scanner input = new java、util、Scanner(System、in); System、out 、print(Enter ten double numbers: ); for (int i = 0; i numbers、length; i+) numbersi = input、nextDouble(); System、out 、println(The min is + min(numbers); public static double min(double list) double min = list0; for (int i =
23、 1; i listi) min = listi; return min; (5) 编写一个方法 , 求整数矩阵中所有整数的与, 使用下面的方法头 : Public static double sumMatrix(int m) 编写一个测试程序 , 读取一个 4 X 4 的矩阵 , 然后显示所有元素的与。精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 8 页,共 22 页 - - - - - - - - - - java 实验题(5)import java、util、Scanner; public cla
24、ss Exercise7_1 public static void main(String args) Scanner input = new Scanner(System、in); System、out 、print(Enter a 4 by 4 matrix row by row: ); double m = new double44; for (int i = 0; i 4; i+) for (int j = 0; j 4; j+) mij = input、nextDouble(); System、out 、print(Sum of the matrix is + sumMatrix(m
25、); public static double sumMatrix(double m) int sum = 0; for (int i = 0; i m、length; i+) for (int j = 0; j m0、length; j+) sum += mij; return sum; 试验四对象与类(1)( 矩形类Rectangle) 遵从 8、2 节中 Circle类的例子 , 设计一个名为Rectangle 的类表示矩形。这个类包括: 1) 两个名为 width 与 height的 double 型数据域 , 它们分别表示矩形的宽与高。width 与 height 的默认值都就是 1
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022 java 实验
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内