2022年浙大《Java语言程序设计》编程答案整理 .pdf





《2022年浙大《Java语言程序设计》编程答案整理 .pdf》由会员分享,可在线阅读,更多相关《2022年浙大《Java语言程序设计》编程答案整理 .pdf(9页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、实验 5 分支结构程序的设计程序填空,不要改变与输入输出有关的语句。一、显示两级成绩输入一个正整数repeat (0repeat10),做 repeat次下列运算:输入一个学生的数学成绩,如果它低于60,输出“ Fail ”,否则,输出“ Pass”。输出使用 System.out.println(Pass);及 System.out.println(Fail);例:括号内是说明输入2 (repeat=2) 60 59 输出Pass Fail import java.util.Scanner; public class Test30001 public static void main(Str
2、ing args) int ri, repeat; int mark; Scanner in=new Scanner(System.in); repeat=in.nextInt(); for(ri=1; ri=repeat; ri+) mark=in.nextInt(); /*-*/ if (mark60) System.out .println(Fail); else System.out .println(Pass); 二、找最小值输入一个正整数repeat (0repeat10),做 repeat次下列运算:输入四个整数,输出其中的最小值。例:括号内是说明输入3 (repeat=3) 1
3、2 6 1 90 10 40 30 20 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 9 页 - - - - - - - - - -1 -3 -4 -5 输出min is 1 (12 6 1 90 中的最小值是1) min is 10 (10 40 30 20 中的最小值是10) min is -5 (-1 -3 -4 -5中的最小值是-5) import java.util.Scanner; public class Test30002 public static
4、void main(String args) int ri, repeat; int a, b, c, d, min; Scanner in=new Scanner(System.in); repeat=in.nextInt(); for(ri=1; rib) min=b; if(minc) min=c; if(mind) min=d; System.out.println(min is +min); 三、求三角形的面积和周长输入一个正整数repeat (0repeat10),做 repeat次下列运算:输入三角形的三条边a, b, c,如果能构成一个三角形,输出面积area 和周长 peri
5、meter ;否则,输出“ These sides do not correspond to a valid triangle”。在一个三角形中,任意两边之和大于第三边。三角形的面积计算公式:area*area = s(s-a)(s-b)(s-c) 其中 :s = (a+b+c)/2 输出代码 : System.out.println(area=+(int)(area*100+0.5)/100.+;perimeter=+(int)(perimeter*100+0.5)/100.); System.out.println(These sides do not correspond to a va
6、lid triangle);例:括号内是说明输入名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 9 页 - - - - - - - - - 2 (repeat=2) 5 5 3 1 1 4 输出area=7.15;perimeter=13.0 These sides do not correspond to a valid triangle import java.util.Scanner; public class Test30003 public static voi
7、d main(String args) int ri, repeat; float a, b, c, area, perimeter, s; Scanner in=new Scanner(System.in); repeat=in.nextInt(); for(ri=1; ric&a+cb&b+ca) / 注意:复合条件的正确写法 s=(a+b+c)/2; area=(float)Math.sqrt(s*(s-a)*(s-b)*(s-c); perimeter=a+b+c; System.out .println(area=+( int)(area*100+0.5)/100.+;perimet
8、er=+( int)(perimeter*100+0.5)/100.); elseSystem.out .println(These sides do not correspond to a valid triangle); 四、判断数的符号输入整数 x,若 x 大于 0,y=1;若 x 等于 0,y=0;否则, y=-1 ,最后输出y。例:括号内是说明输入3 (repeat=3) 2 -8 0 输出1 (x=2时 y=1) -1 (x=-8时 y=-1) 0 (x=0时 y=0) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - -
9、 名师精心整理 - - - - - - - 第 3 页,共 9 页 - - - - - - - - - import java.util.Scanner; public class Test30004 public static void main(String args) int ri, repeat; int x, y; Scanner in=new Scanner(System.in); repeat=in.nextInt(); for(ri=1; ri0) y=1; elseif(x=0) y=0; / 注意: =是赋值运算符, =才是比较相等的运算符else y=-1; System
10、.out.println(y); 五、计算个人所得税输入一个正整数repeat (0repeat10),做 repeat次下列运算:输入一个职工的月薪salary ,输出应交的个人所得税tax 。tax = rate * (salary-850) 当 salary = 850 时, rate = 0%; 当 850 salary = 1350 时, rate = 5%; 当 1350 salary = 2850 时, rate = 10%; 当 2850 salary = 5850 时, rate = 15%; 当 5850 salary 时, rate = 20%; 例:括号内是说明输入5
11、(repeat=5) 1010.87 32098.76 800 4010 2850 输出tax=8.04 tax=6249.75 tax=0.0 tax=474.0 tax=200.0 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 9 页 - - - - - - - - - import java.util.Scanner; public class Test30005 public static void main(String args) int ri, repea
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java语言程序设计 2022年浙大Java语言程序设计编程答案整理 2022 浙大 Java 语言程序设计 编程 答案 整理

限制150内