首页 技术 正文
技术 2022年11月20日
0 收藏 560 点赞 2,379 浏览 1344 个字

[Java] 实验7參考代码,代码已更新。感兴趣的同学能够去学习。

1. default package问题可參考实验6

2. for, if, while等。后面包括多条语句时,须要用花括号括起来

3. 为什么须要close scanner, 可參考实验6 (已简要更新原因)

 

40035 输出某月的天数

1. 较简便的switch-case的写法

switch(month) {
case 1:
case 3:
// Output 31 when month equals 1 or 3
System.out.println(31);
break;
case 2:
// todo. Note that you can use "if" statement in "case"
case 4:
case 6:
// ...
}

We have the coding principle: DRY (Do not repeat yourself.)

2. 闰年的定义

满足例如以下条件中,随意一条。即为闰年。考虑年份n,

    – 若n能被4整除。但不能被100整除

    – 若n能被400整除

3. 本地执行正确,提交“答案错误”的同学,能够考察例如以下case:

year = 1900, month = 2

year = 2000, month = 2

大家要自己设计一些case, 去验证“特殊情况”、“边际条件”等。

 

40037 计算不及格的人数

1. 程序怎样停止,请类比40008. 求奇数和那题。

更新:该题代码已在实验6 參考代码中给出。

2. 在第二行输出结果错误(平均分输出73.75, 或者不及格人数输出3)的同学,能够參考实验六“求1+1/2+1/3+……+1/n”那题,第3点。

通俗版代码:(我还没有原题相应的程序。待补充)

进阶代码:

import java.util.Scanner;public class GradeStatistics {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int repeat = in.nextInt();
while (repeat-- != 0) {
double gradeTot = 0; // gradeTotal, the sum of the input grades
int gradeCnt = 0; // gradeCount, the number of the input grades
int failCnt = 0; // failCount, the number of the grades, which is < 60
for (int grade = in.nextInt(); grade >= 0; grade = in.nextInt()) {
gradeTot += grade;
++ gradeCnt;
failCnt += grade < 60?1: 0;
}
double average = gradeTot / gradeCnt;
System.out.println("average=" + Math.round(average * 100) / 100d);
System.out.println("count=" + failCnt);
}
}
}

50001

要编写函数fact, 注意到,函数主要由:返回值、函数名、參数列表、函数体组成。

不理解的同学能够自行查阅參考书。

50002

这题要求定义的函数fn, 它的參数列表是要填写两个參数的。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849