用java解决输入多个年份,年份用空格隔开,输入遇到-1结束,输出闰年的个数
时间: 2024-03-23 07:38:45 浏览: 21
好的,我可以为您提供代码实现。
```java
import java.util.Scanner;
public class LeapYearCount {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int count = 0;
while (true) {
int year = scanner.nextInt();
if (year == -1) {
break;
}
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
count++;
}
}
System.out.println("闰年的个数为:" + count);
}
}
```
以上代码可以实现输入多个年份,年份用空格隔开,输入遇到-1结束,输出闰年的个数。您可以将它复制粘贴到您的Java开发环境中进行运行。
相关问题
用java解决输入多个年份,年份之间用空格隔开,输入遇到-1结束,输出闰年的个数
好的,您的问题已经收到。下面是Java代码实现:
```java
import java.util.Scanner;
public class LeapYearCounter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int count = 0;
while (true) {
int year = scanner.nextInt();
if (year == -1) {
break;
}
if (isLeapYear(year)) {
count++;
}
}
System.out.println("闰年的个数是:" + count);
}
private static boolean isLeapYear(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return true;
}
return false;
}
}
```
运行程序后,您可以输入多个年份,每个年份之间用空格隔开。当您输入-1时,程序会停止输入,并输出闰年的个数。
用python,输入只有一行,用两个空格隔开的整数,代表两个年份,若m是闰年,n也是闰年输出1
可以使用以下代码来实现:
```python
def check_leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
return True
else:
return False
input_str = input("请输入两个年份,用两个空格隔开:")
year1, year2 = map(int, input_str.split())
if check_leap_year(year1) and check_leap_year(year2):
print(1)
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)