7
votes

Calculer l'année saut en Java

Dupliqué possible: strong>
code Java pour calculer l'an biss, est ce code correct? p>

C'était des devoirs et j'ai déjà reçu ma note, mais je n'ai pas mis en œuvre des années de saut dans mon code. Il s'agit d'un programme simple qui affiche les chiffres dans un mois en fonction de la saisie de l'utilisateur. La seule chose est que je ne peux pas comprendre est un moyen de mettre en œuvre une année bissextile qui obtiendrait 29 jours pour le février au lieu de 28 ans, sans écrire plusieurs si code> déclarations. Sûrement il y a un moyen plus facile? Voici le code: P>

//Displays number of days in a month

package chapter_3;

import java.util.Scanner;


public class Chapter_3 {

    public static void main(String[] args) {
        System.out.println("This program will calculate \n"
                + "the number of days in a month based upon \n"
                + "your input, when prompted please enter \n"
                + "the numerical value of the month you would like\n"
                + "to view including the year. Example: if you would like\n"
                + "to view March enter 3 2011 and so on.");

        Scanner input = new Scanner(System.in);

        //Prompt user for month and year
        System.out.print("Please enter the month and year: ");
        int month = input.nextInt();
        int year = input.nextInt();

        if (month == 1) {
            System.out.println("January " + year + " has 31 days");
        }   
        else if (month == 2) {
            System.out.println("February " + year + " has 28 days");
        }   
        else if (month == 3) {
            System.out.println("March " + year + " has 31 days");
        }
        else if (month == 4) {
            System.out.println("April " + year + " has 30 days");
        }
        else if (month == 5) {
            System.out.println("May " + year + " has 31 days");
        }
        else if (month == 6) {
            System.out.println("June " + year + " has 30 days");
        }
        else if (month == 7) {
            System.out.println("July " + year + " has 31 days");
        }
        else if (month == 8) {
            System.out.println("August " + year + " has 31 days");
        }
        else if (month == 9) {
            System.out.println("September " + year + " has 30 days");
         }
        else if (month == 10) {
            System.out.println("October " + year + " has 30 days");
        }
        else if (month == 11) {
            System.out.println("November " + year + " has 30 days");
        }
        else if (month == 12) {
            System.out.println("December " + year + " has 31 days");
         }
        else {
            System.out.println("Please enter the numerical value "
                + "of the month you would like to view");
        }
    }
}        


1 commentaires

3 Réponses :


14
votes

citant http://en.wikipedia.org/wiki/leap_year

Des années de divisibles de manière uniforme par 100 ne sont pas des années saut, à moins qu'ils sont également uniformément divisibles de 400, auquel cas ils sont des années saut. P> blockquote>

Donc, si vous voulez réinventer la roue: p> xxx pré>

mais la solution la plus élégante (prise de ICI ) est probablement P>

public static boolean isLeapYear(int year) {
  Calendar cal = Calendar.getInstance();
  cal.set(Calendar.YEAR, year);
  return cal.getActualMaximum(DAY_OF_YEAR) > 365;
}


0 commentaires

3
votes

Vous pouvez utiliser une déclaration de commutation, avec quelques instructions à l'intérieur du cas de février: xxx

voir http://download.oracle.com/javase/tutorial/java/nutsandbolts/java/nutsandbolts/java.html Pour plus d'informations


0 commentaires