lundi 30 mai 2016

3 conditions in one if statement

this programm is supposed to check if the entered year is a leap year or not. But I'm already running in errors while compiling. The formula to check if it's leap year or not is the following:

If you can divide the year by 4 it's a leap year unless you can at the same time also divide it by 100, then it's not a leap year unless you can at the same time divide it by 400 then it's a leap year.

 public class Schaltjahr {
 public static void main (String[] args) {
     double d;
     String eingabe;
     eingabe = JOptionPane.showInputDialog("Gib das Jahr ein "); //Type in the year
     d = Double.parseDouble(eingabe);
     if ((d % 4 == 0) & (d % 100 == 0) && (d % 400 = 0) ) { 
         JOptionPane.showMessageDialog(null,"Es ist ein Schaltjahr"); //It is a leap year
     } else {
         if ((d % 4 == 0) & (d % 100 == 0) )) {
              JOptionPane.showMessageDialog(null, "Es ist kein Schaltjahr"); //It is not a leap year
         }    
     } else {
         if (d % 4 == 0) {
             JOptionPane.showMessageDialog(null, "Es ist ein Schaltjahr"); // It is a leap year
         }
     }
     }
 }

While compiling i get this error:

 Schaltjahr.java:16: error: illegal start of expression
             if ((d % 4 == 0) & (d % 100 == 0) )) {
                                                ^
Schaltjahr.java:19: error: 'else' without 'if'
         } else {
           ^
2 errors

I'm a total beginner and this is an exercise from my book. I lost the CD with the Source of it.

Aucun commentaire:

Enregistrer un commentaire