代码展示:
public static void main(String[] args) {
int year = 1988;
while (year<2020){
if(year%4==0 && year%100!=0 || year%400==0 ){
System.out.println(year+"是闰年");
}
year++;
}
}
6. 有一个容量为10L的空水桶。水桶的上面开始往里灌水,同时下面开始往出流水。第一分钟灌水的速度是1L/min,第
// 二分钟灌水的速度是2L/min,第三分钟灌水的速度是3L/min,以此类推。而流水的速度固定是3L/min。那么几分钟之后,
// 水桶里能保持灌满水的状态?
代码展示:
public static void main(String[] args) {
int temp=0; //现有水量
int count =0; //计算分钟
while (temp<10){
count++;
temp =temp + count -3;
if(temp <0){
temp =0; //前三分钟 桶里都是无水的
}
}
System.out.println(count);
}
7. 定义 int a = 5 b = 3 c = 8
//1、利用if语句获取最小值打印
//2、利用三元运算符获取最大值打印
代码展示:
public static void main(String[] args) {
int a = 5, b = 3, c = 8;*/
//第①个
/*int temp = a < b ? a : b;
int min = temp < c ? temp : c;
System.out.println("最小值为:" + min);*/
//第②个
/*if (a < b && a < c) {
System.out.println("最小值为:" + a);
} else if (b < a && b < c) {
System.out.println("最小值为:" + b);
} else {
System.out.println("最小值为:" + c);
}
}
8. 获取10~99(包含10和99)的“总和”与“偶数”的个数,并在控制台打印输出
代码展示:
public static void main(String[] args) {
int count = 0;
for (int i = 10; i <= 99; i++) {
if (i % 2 == 0){
System.out.println(i);
}
count++;
}
System.out.println("10~99共有" + count + "个");
}
代码展示:
public static void main(String[] args) {
for (int i = 1; i < 6; i++) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
14.分析以下需求,并用代码实现:
// *
// ***
//*****
代码展示:
public static void main(String[] args) {
for (int i=1 ;i<6;i++){
for (int j=0; j<i;j++){
if(i%2==0){
continue;
}
System.out.print("*");
}
System.out.println();
}
}
15. 键盘录入任意一个整数,程序能输出这个数的位数。例如输入123 输出:123是一个3位数
代码展示:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
int number1 =number;
int count = 1;
while (number>0){
if(number<10){
System.out.println(number1 + "是"+ count+"位数");
break;
} else if (number >10){
number /=10;
count++;
}
}
}
16.键盘录入任意一个整数,程序能输出这个数的位数。例如输入123 输出:123是一个3位数
代码展示:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
int number1 = number;
int count = 1;
while (number > 0) {
if (number < 10) {
System.out.println(number1 + "是" + count + "位数");
break;
} else if (number > 10) {
number /= 10;
count++;
}
}
}
17.要求输出1到10000中所有既能被3整除又能被7整除的偶数
代码展示:
public static void main(String[] args) {
for (int i = 1; i <= 10000; i++) {
if(i%3==0 && i%7==0){
System.out.println(i);
}
}
}、
// 第十九题:要求求出1到100中所有既能被3整除又能被7整除的偶数的和sum1,以及十位
// 数为7并且能被3整除的所有的奇数的和sum2。要求最后输出sum1和sum2的差值
/*public static void main(String[] args) {
int sum1 = 0;
int sum2 = 0;
for (int i = 1; i <= 101; i++) {
if (i % 3 == 0 && i % 7 == 0 && i % 2 == 0) {
sum1 += i;
}
if (i / 10 % 10 == 7 && i % 3 == 0 && i % 2 != 0) {
sum2 += i;
}
}
System.out.println("sum1:"+ sum1 + " sum2:" + sum2);
}*/
}