java 1-16笔记

例题分析:
1.要判断一个年份是否是闰年
(1)首先某个年份如果能被4整除同时还要被100整除或者能被400整除则为闰年
(2)然后我们可以用一个if函数就可以解决

import java.util.Scanner;
public class Demo18 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//1.提示用户输入一个年份
System.out.print("Enter a year:");
int year = input.nextInt();
//2.判断其是否是闰年
if ((year % 4 == 0 && year % 100 !=0) || (year % 400 == 0)) {
System.out.println(year + "是闰年");
}
System.out.println("判断结束!");
}
}

2.在这里插入图片描述
先找数字特点:
第4行 4 3 2 1 2 3 4
3 2 1 0 1 2 3
y = |x| + 1 x∈[-3,3]
第5行 5 4 3 2 1 2 3 4 5
4 3 2 1 0 1 2 3 4
y = |x| + 1 x∈[-4,4]
第i行 y = |x| + 1 x∈[-(i-1),i-1]
然后就可以用俩个for循环可以写出还需要一个for来控制

public class Demo41 {
public static void main(String[] args) {

for (int i = 1; i <= 7; i++) {
for (int k = 1; k <= 7 - i ;k++) {
System.out.print(" ");
}
for (int j = -(i - 1); j <= i - 1; j++) {
System.out.print(Math.abs(j) + 1 + " ");
}
System.out.println();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值