Note that there are some explanatory texts on larger screens.

plurals
  1. POMy boolean statement works one way but not in a seemingly unchanged way
    primarykey
    data
    text
    <p>In an attempt to make my code a bit more aesthetic and short, I tried to change my code from this:</p> <pre><code>do { System.out.print("Enter day: "); day = input.nextInt(); if ((isLeapYear &amp;&amp; month == 2 &amp;&amp; (day &lt; 1 || day &gt; 29)) || (!isLeapYear &amp;&amp; month == 2 &amp;&amp; (day &lt; 1 || day &gt; 28)) || ((month == 4 || month == 6 || month == 9 || month == 11) &amp;&amp; (day &lt; 1 || day &gt; 30)) || ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month ==12) &amp;&amp; (day &lt; 1 || day &gt; 30))) { System.out.println("Invalid input"); } } while ((isLeapYear &amp;&amp; month == 2 &amp;&amp; (day &lt; 1 || day &gt; 29)) || (!isLeapYear &amp;&amp; month == 2 &amp;&amp; (day &lt; 1 || day &gt; 28)) || ((month == 4 || month == 6 || month == 9 || month == 11) &amp;&amp; (day &lt; 1 || day &gt; 30)) || ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month ==12) &amp;&amp; (day &lt; 1 || day &gt; 30))); </code></pre> <p>to this:</p> <pre><code>boolean invalidDay = (isLeapYear &amp;&amp; month == 2 &amp;&amp; (day &lt; 1 || day &gt; 29)) || (!isLeapYear &amp;&amp; month == 2 &amp;&amp; (day &lt; 1 || day &gt; 28)) || ((month == 4 || month == 6 || month == 9 || month == 11) &amp;&amp; (day &lt; 1 || day &gt; 30)) || ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month ==12) &amp;&amp; (day &lt; 1 || day &gt; 30)); do { System.out.print("Enter day: "); day = input.nextInt(); if (invalidDay) { System.out.println("Invalid input"); } } while (invalidDay); </code></pre> <p>The second one does not work as intended but the first way works perfectly. The only thing I have changed is putting the long boolean expression into a single boolean variable. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload