Java学习资料,ppt,复习资料(2)

2020-02-21 21:00

17 runNian = false; // 条件表达式为假,是平年 18

}

19 // 根据runNian的值,输出 20 21 22 23

if (runNian) {

System.out.println(year + \年闰年\} else {

24 System.out.println(year + \年平年\

/ 26 /* 计算当月的天数 */

/ / / / / / / / / / / / / / / / / / / / / /

27 switch (month) {

28 case 1: // 1、3、5、7、8、10、12这7个月的天数相同 29 case 3:

30 case 5:

31 case 7: 32 case 8: 33 case 10: 34 case 12:

35 days=31; 36 break; 37 case 2:

38 if (runNian) { // 2月的天数因平年或是闰年而不同 39 40 41 42 43 25 45 46 47

days = 29; } else { days = 28; } break; }44 default: days = 30; break; }

48 System.out.println(month + \月\\t共\天\

49 inttotalDays = 0; //totalDays输入年份之前的总天数 50 inti = 1;

51 while (i< year) { //使用循环累加从公元1年到(year-1)年的总天数 52 if (i % 4 == 0 && !(i % 100 == 0) || i % 400 == 0) { //判断是否为闰年

53 totalDays = totalDays + 366; //闰年366天 54 55

} else {

56 totalDays = totalDays + 365; //平年365天 57 } 58 i++; 59 }

60 System.out.println(year + \年之前的总天数:\天\61 intbeforeDays = 0; //year年中month月前几个月的天数 62 i = 1;

63 do { //从1月到(month-1)月的天数 64

switch (i) {

65 case 1: // 1、3、5、7、8、10、12这7个月的天数相同 66 67 68 69 70 71 72 73 74

case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 2:

if (runNian) { // 2月的天数因平年或是闰年而不同

75

76 days = 29; 77 } 78 else { 79 days = 28; 80 } 81 82 83 84

break;

default:

days = 30; break;

85 } 86 87

if (i< month) { beforeDays = beforeDays + days;

88 } 89 i++;

90 }while (i<= month);

91 totalDays = totalDays + beforeDays;

92 System.out.println(year+\年\月之前的总天数:\天\93 System.out.println(\当前月份的天数:\天\

94 /*存储当月第一天是星期几:0为星期日,1~6为星期一~星期六*/ 95 96 97 98 99 100 101 102 103 105 106

intfirstOfMonth = (1 + totalDays )% 7;

/* 输出日历 ‘\\t’是制表符,相当于table键*/

System.out.println(\星期日\\t星期一\\t星期二\\t星期三\\t星期四\\t星期五\\t星期六\ for (intnulNum = 0; nulNum

//该月第一天之前要输出的空格数,如果是星期五,前面要输出5个空格

System.out.print(\ //输出空格 }

for (int j = 1; j <= days; j++) { //在输完空格的位臵上输出本月的日历 System.out.print(j+ \ if ((totalDays + j) % 7 == 6) { // 如果当天为周六,输出换行

}

System.out.println();

? 107 }

?

? 根据学生的成绩,判定其优秀等级。

1 import java.util.Scanner; 2 public class CountGrade {

3 public static void main(String[] args) { 4 int score = 0; // 学生成绩 5 int num = 0; // 90分以上的科目数

6 Scanner input = new Scanner(System.in); 7 System.out.println(\请输入各门成绩:\8 for (int i = 0; i < 3; i++) { //统计90分以上的科目数 9 score = input.nextInt(); //接收数据 10 if (score < 60) { //成绩低于60分时 11 System.out.println(\别泄气,加油!\12 num = -1; // num臵为-113 break; //结束循环 14 } else if (score < 90) { // 90分以下的科目不计数 15 continue; //结束本次循环,不再执行循环体内下面的语句 16 17

} else {

num = num + 1; } //成绩>90,num加1

18 }

19 switch (num) { //根据num值判定等级 20 case 1:

21 System.out.print(\该生的优秀级别为:☆\22 break; 23 case 2:

24 System.out.print(\该生的优秀级别为:☆☆\25 break;26 case 3:

27 System.out.print(\该生的优秀级别为:☆☆☆\28 29 30 31

break; default: break; } }

32 }

保存程序为CountGrade.java,程序运行结果如下: 请输入各门课成绩:

96 75 56

别泄气,加油!

兔子生兔子

1 /** Fibonacci数列前20项的输出**/ 2 public class ArrayFibonacci {

3 public static void main(String args[]) { 4 5 6

int Fibonacci[]; // 声明一维数组 int i;

Fibonacci = new int[20]; // 创建一维数组

7 Fibonacci[0] = 1; 8 Fibonacci[1] = 1; 9

System.out.println(\数列前20项的输出--------\// 给数组元素赋值

10

11 for (i = 2; i < Fibonacci.length; i++)

12 Fibonacci[i] = Fibonacci[i - 1] + Fibonacci[i - 2]; 13 14 15 16 17 18 19

// 输出数组元素

for (i = 0; i < Fibonacci.length; i++) { System.out.print(Fibonacci[i] + \ if ((i + 1) % 5 == 0) System.out.println(\ } }


Java学习资料,ppt,复习资料(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:《信息技术及其发展趋势》 教学设计

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: