Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用布尔效用法计算多个年份以找到闰年_Java_Gregorian Calendar_Leap Year - Fatal编程技术网

Java 使用布尔效用法计算多个年份以找到闰年

Java 使用布尔效用法计算多个年份以找到闰年,java,gregorian-calendar,leap-year,Java,Gregorian Calendar,Leap Year,各位下午好,, 我在用布尔效用法计算多个闰年时遇到了一个问题。我被告知要使用循环,但很难弄清楚如何以及在何处放置循环 import java.util.Scanner; public class LeapYear { private static boolean isLeapYear(int year) { if (year % 400 == 0 && year % 100 == 0) { return true;

各位下午好,, 我在用布尔效用法计算多个闰年时遇到了一个问题。我被告知要使用循环,但很难弄清楚如何以及在何处放置循环

import java.util.Scanner;

 public class LeapYear {
      private static boolean isLeapYear(int year) {
          if (year % 400 == 0 && year % 100 == 0) {
              return true;
          }
          if (year % 100 == 0) {
              return false;
          }
          if (year % 4 == 0) {
              return true;
          }
          else {
             return false;
         }
     }
public static void main(String[] args) {
    int quitter = 0;
    int negative = 0;
    int year = 0;

    Scanner scan = new Scanner(System.in);

    System.out.println("Enter a year or -1 to quit: ");
    year = scan.nextInt();

    while (year != -1 && year < 1582) {
            System.out.print("The Gregorian calendar was not adopted until 1582, please enter a year after 1582: ");
            year = scan.nextInt();
    }

    if(year == -1) {
        negative += year;
        quitter++;
    }

    System.out.println(isLeapYear(year));
}
import java.util.Scanner;
公共课年{
私有静态布尔值isLeapYear(整数年){
如果(年份%400==0和年份%100==0){
返回true;
}
如果(年份%100==0){
返回false;
}
如果(第%4年==0){
返回true;
}
否则{
返回false;
}
}
公共静态void main(字符串[]args){
int-quitter=0;
int负=0;
整年=0;
扫描仪扫描=新扫描仪(System.in);
System.out.println(“输入年份或-1退出:”;
年份=scan.nextInt();
而(年份!=-1&&year<1582){
System.out.print(“1582年才采用公历,请输入1582年之后的年份:”);
年份=scan.nextInt();
}
如果(年份==-1){
负+=年;
quitter++;
}
系统输出打印项次(isLeapYear(年));
}

}

只需使用do while循环:

public static void main(String[] args) {
    int year = 0;
    Scanner scan = new Scanner(System.in); 
    do{
        System.out.println("Enter a year or -1 to quit: ");
        year = scan.nextInt();
        while (year < 1582) { 
            System.out.print("The Gregorian calendar was not adopted until 1582, please enter a year after 1582: ");
            year = scan.nextInt(); 
        } 
        if(year != -1) { 
            System.out.println(isLeapYear(year));
        } 
    }while(year!=-1);
}
publicstaticvoidmain(字符串[]args){
整年=0;
扫描仪扫描=新扫描仪(System.in);
做{
System.out.println(“输入年份或-1退出:”;
年份=scan.nextInt();
而(年份<1582){
System.out.print(“1582年才采用公历,请输入1582年之后的年份:”);
年份=scan.nextInt();
} 
如果(年!=-1){
系统输出打印项次(isLeapYear(年));
} 
}而(年份!=-1);
}

刚刚意识到最后一个“}”没有包含在代码中,我保证是的。如果
year%100==0,它不应该是
false
吗?哦。是的,应该是。您是否知道如何评估多年?您可以通过编辑您的帖子轻松更正该“}”